Description

The twenty-first century is a biology-technology developing century. One of the most attractive and challenging tasks is on the gene project, especially on gene sorting program. Recently we know that a gene is made of DNA. The nucleotide bases from which DNA is built are A(adenine), C(cytosine), G(guanine), and T(thymine). Given several segments of a gene, you are asked to make a shortest sequence from them. The sequence should use all the segments, and you cannot flip any of the segments.

For example, given 'TCGG', 'GCAG', 'CCGC', 'GATC' and 'ATCG', you can slide the segments in the following way and get a sequence of length 11. It is the shortest sequence (but may be not the only one). 

Input

The first line is an integer T (1 <= T <= 20), which shows the number of the cases. Then T test cases follow. The first line of every test case contains an integer N (1 <= N <= 10), which represents the number of segments. The following N lines express N segments, respectively. Assuming that the length of any segment is between 1 and 20.

Output

For each test case, print a line containing the length of the shortest sequence that can be made from these segments.

Sample Input

1
5
TCGG
GCAG
CCGC
GATC
ATCG

Sample Output

11

【题意】找出最短的字符串长度,要求包含给出的所有字符串

#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
const int inf=0x7777777;
int n,maxn;
int mp[][],ln[],vis[],next1[][];
char s[][];
int kmp(int x,int y)
{
int i=,j=;
while(i<ln[x])
{
if(s[x][i]==s[y][j])
{
i++;j++;
}
else
{
if(j==) i++;
else
{
j=next1[x][j];
}
}
if(j==ln[y]) return -;//能在x中找到y;
}
return j;
}
void get_next(int k)
{
int j=-;
next1[k][]=-;
for(int i=;i<ln[k];i++)
{
if(j==-||s[k][i]==s[k][j]) next1[k][++i]=++j;
else j=next1[k][j];
}
}
void dfs(int len,int cnt,int k)
{
if(cnt==n)//已经是第n个字符串了,获取最小的长度
{
if(len<maxn) maxn=len;
return;
}
for(int i=;i<=n;i++)
{
if(vis[i]==)
{
vis[i]=;
if(mp[k][i]==-)//如果字符串k和i包含关系
{
dfs(len,cnt+,k);//最后一个参数仍然是k
}
else dfs(len+ln[i]-mp[k][i],cnt+,i);//如果不包含,两个串的长度相加减去相同部分长度
vis[i]=;
}
}
return;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%s",s[i]);
ln[i]=strlen(s[i]);
get_next(i);//求next数组
}
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
mp[i][j]=kmp(i,j);//求两个字符串的匹配度
}
}
maxn=inf;
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++)
{
vis[i]=;
dfs(ln[i],,i);
vis[i]=;
}
printf("%d\n",maxn);
}
return ;
}

Best Sequence_DFS&&KMp的更多相关文章

  1. KMP算法求解

    // KMP.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> using namespac ...

  2. 简单有效的kmp算法

    以前看过kmp算法,当时接触后总感觉好深奥啊,抱着数据结构的数啃了一中午,最终才大致看懂,后来提起kmp也只剩下“奥,它是做模式匹配的”这点干货.最近有空,翻出来算法导论看看,原来就是这么简单(先不说 ...

  3. KMP算法

    KMP算法是字符串模式匹配当中最经典的算法,原来大二学数据结构的有讲,但是当时只是记住了原理,但不知道代码实现,今天终于是完成了KMP的代码实现.原理KMP的原理其实很简单,给定一个字符串和一个模式串 ...

  4. 萌新笔记——用KMP算法与Trie字典树实现屏蔽敏感词(UTF-8编码)

    前几天写好了字典,又刚好重温了KMP算法,恰逢遇到朋友吐槽最近被和谐的词越来越多了,于是突发奇想,想要自己实现一下敏感词屏蔽. 基本敏感词的屏蔽说起来很简单,只要把字符串中的敏感词替换成"* ...

  5. [KMP]【学习笔记】

    Oulipo Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36916   Accepted: 14904 Descript ...

  6. KMP算法实现

    链接:http://blog.csdn.net/joylnwang/article/details/6778316 KMP算法是一种很经典的字符串匹配算法,链接中的讲解已经是很明确得了,自己按照其讲解 ...

  7. KMP专题

    1.[HDU 3336]Count the string(KMP+dp) 题意:求给定字符串含前缀的数量,如输入字符串abab,前缀是a.ab.aba.abab,在原字符串中出现的次数分别是2.2.1 ...

  8. KMP学习之旅

    说起kmp就要从字符串的匹配说起,下面我们谈谈字符串的匹配 给定一个原字符串:bababababababababb,再给定一个模式串:bababb,求模式串是否在源字符串中出现 最简单的方法就是遍历源 ...

  9. KMP模板

    参考:http://www.cnblogs.com/c-cloud/p/3224788.html #include<stdio.h> #include<string.h> vo ...

随机推荐

  1. java多线程的常用方法(以及注意事项)

    /* * 线程的常用方法 * 1.start(); * 2.run(); * 3.sleep(int millsecond); * 4.isAlive(); -->判断线程是否还在运行 * 5. ...

  2. Spring配置文件解析--bean属性

    1.bean设置别名,多个别名用逗号隔开 <!--使用alias--> <bean id="app:dataSource" class="...&quo ...

  3. html5的标签

    1.article与section div的区别 当一个标签只是为了样式化或者方便脚本使用时,应该使用 div . section 表示一段专题性的内容,一般会带有标题.,section 应用的典型场 ...

  4. PL/SQL设置编码方式

    (2012-10-30 21:38:33) 转载▼ 标签: 杂谈 分类: ORACLE 导出sql文件出现乱码问题,百度之后,发现问题是由于PL/SQL客户端和ORACLE的字符编码设置不一致引起的. ...

  5. C++ STL pair

    没有找到priority_queue里存放pair不用typedef的方法...大概第一次觉得这个有用吧... 优先队列里和sort函数对pair 的默认排序是first从小到大,second从小到大 ...

  6. 和小猪一起搞微信公众号开发—获取Access_token

    前言 前一篇小猪和大家分享了如何回复用户的简单文本,这一篇我们来看看如何获取Access_token 介绍 在前一篇中,我们实现了这么一个简单的过程:用户发送一个文本到公众号后,公众号在该文本后面加上 ...

  7. [示例]NSDictionary编程题-字典的排序应用(iOS7班)

    代码: #import <Foundation/Foundation.h> int main(int argc, const char * argv[]) { @autoreleasepo ...

  8. Pjax的使用

    什么是pjax? 现在很多网站( facebook,  twitter)都支持这样的一种浏览方式, 当你点击一个站内的链接的时候, 不是做页面跳转, 而是只是站内页面刷新. 这样的用户体验, 比起整个 ...

  9. MVC 3个重要的描述对象之ControllerDescriptor

    1.ControllerDescriptor 1.1 ReflectedControllerDescriptor public class HomeController : Controller { ...

  10. http://www.html-js.com/article/2328

    React.js编程思想 张小俊128 发布在使用React.js开发web应用2015年8月7日view:33385React 在文章任何区域双击击即可给文章添加[评注]!浮到评注点上可以查看详情. ...