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. uva--1339 - Ancient Cipher(模拟水体系列)

    1339 - Ancient Cipher Ancient Roman empire had a strong government system with various departments, ...

  2. Permutation Sequence [LeetCode]

    The set [1,2,3,…,n] contains a total of n! unique permutations. By listing and labeling all of the p ...

  3. backbonejs中的模型篇(二)

    一:模型标识符 每个模型都有一个用作唯一标识符的ID属性,以便在不同模型间进行区分.通过id属性我们可以直接访问模型对象当中用于标识符存放的属性,默认属性名为id,但也可以通过设置idAttribut ...

  4. I-MooFest(POJ 1990)

    MooFest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5697   Accepted: 2481 Descripti ...

  5. cisco nat

    1.配置端口: int s0/0 ip nat outside                  ;s0/0为外部端口 int f0/0 ip nat inside                   ...

  6. Java中的String与常量池[转帖]

    string是java中的字符串.String类是不可变的,对String类的任何改变,都是返回一个新的String类对象.下面介绍java中的String与常量池. 1. 首先String不属于8种 ...

  7. redhat enterprixe 5.0 web 服务配置与管理

    一.Web服务及工作原理 Web服务的实现采用客户/服务器模型.客户机运行Web客户程序(浏览器),作用是解释和显示Web页面,相应用户的输入请求,并且通过http协议将用户请求传递给Web服务器.W ...

  8. 在ASP.NET MVC中使用Area

    前言: 这段时间小猪花了不少功夫在研究ASP.NET MVC的源码上面,可谓思想是了解了不少,用的上用不上却是另外一回事了.! 应用场景: ASP.NET MVC中,是依靠某些文件夹以及类的固定命名规 ...

  9. 从原理上搞定编码(四)-- Base64编码

    开发者对Base64编码肯定很熟悉,是否对它有很清晰的认识就不一定了.实际上Base64已经简单到不能再简单了,如果对它的理解还是模棱两可实在不应该.大概介绍一下Base64的相关内容,花几分钟时间就 ...

  10. 戴文的Linux内核专题:10配置内核(6)

    转自Linux中国 欢迎来到下一篇关于内核配置文章!还有大量的选项需要配置.这篇文章将主要讨论PCI和ACPI. 这里我们可以启用由ACPI控制的扩展坞和可移动驱动器槽的支持(Dock).记住,ACP ...