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. jsp有关resquest与session和application的区别和相似性

    1. request 的setAttribute与getAttribute方法一般都是成对出现的,首先通过setAttribute方法设置属性与属性值,然后通过 getAttribute方法根据属性获 ...

  2. Maximal Rectangle [LeetCode]

    Problem Description: Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle co ...

  3. SAP资产变动明细

    *&---------------------------------------------------------------------* *& Report ZFIR027 * ...

  4. 并查集 POJ 1988

    #include <cstdio> #define N 30010 int pa[N]; //parent int siz_tree[N]; //size of tree int d[N] ...

  5. node 事件循环

    什么是事件循环 Node只运行在一个单一线程上,至少从Node.js开发者的角度是这样的.在底层, Node是通过libuv来实现多线程的. Libuv库负责Node API的执行.它将不同的任务分配 ...

  6. java入门第四步之应用服务器的安装(Tomcat)【转】

    首先打开myeclipse,在myeclipse菜单栏下面有两个按钮: 左边的按钮是进行项目的部署的(即将项目部署到服务器上),右边的按钮是进行服务器的启动的,如果你安装了tomcat服务器,那你就可 ...

  7. Spring mvc中使用request和response

    @ResponseBody @RequestMapping(value="/ip", method=RequestMethod.GET) public String getIP(H ...

  8. spring mvc如何获取问号后的url参数

    @RequestMapping(method=RequestMethod.GET) public ModelAndView allUsers(@RequestParam int page){ Mode ...

  9. caches 文件夹删除

    模拟器 可以 删除 真机不行

  10. flask剖析

    1.为何只要通过import request,就能拿到对应的request呢?怎么解决区分请求,区分线程的问题? 简而言之,就是通过拿栈顶对象就表示是当前活动的对象 但对于多线程,由于栈的数据结构是 ...