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. 杂记(编程style)----google code style!

    1.文件名 使用小写字母和下划线组合.头文件以.h结尾,定义文件用.cc结尾.例如:my_useful_class.cc 2.类型名 使用大写字母开头,多个单词组合时每个单词的首字母大写.例如:Url ...

  2. 盘点几种数据库的分页SQL的写法(转)

    Data序列——盘点几种数据库的分页SQL的写法http://www.cnblogs.com/fireasy/archive/2013/04/10/3013088.html

  3. GPOR

    [tengzhenzhen15@lu01 gpor]$ for ((i=0; i<=19; i++)) do ./gpor -S 0.4 X4058_300_gpor/mytask_train. ...

  4. JDE报表开发笔记(数据选择及继承)

    在Section的Event中, Do Custom Section("sectionxxx")自定义加载下一个Section Set Selection Append Flag( ...

  5. BZOJ3307 雨天的尾巴

    首先考虑序列怎么做... 只要把操作差分了,记录在每个点上 然后维护一棵权值线段树,表示每个颜色出现的次数,支持单点修改和查询最大值操作 只要把序列扫一遍就好了,时间复杂度$O(n + m*logZ) ...

  6. JAVA-数据库连接【转】

    SqlServer.Oracle.MySQL的连接,除了地址和驱动包不同,其他都一样的. 1 public String urlString="jdbc:sqlserver://localh ...

  7. 第三方开源框架的下拉刷新列表(QQ比较常用的)。

    PullToRefreshListView是第三方开源框架下拉刷新列表,比较流行的QQ 微信等上面都在用. 下载地址(此开源框架于2013年后不再更新) 点此下载 package com.lixu.k ...

  8. OL/SQL编程练习

    create or replace procedure pr_first is --一个变量 v_a ) := '总有一天我的生命将走到尽头'; --一个常量 c_b constant ) := '而 ...

  9. 用ant进行编译和打包(java)

    ant是目前java环境下最好用的打包部署工具,其采用xml的格式进行编写,功能非常强大.现介绍一下如何手工使用ant进行java程序的编译打包.一.安装ant1.下载并安装ant.到官方主页http ...

  10. 关于equals和hashCode

    equals()和hashCode()是Object类的两个函数,重要性可见一斑,不过我们平时使用却未必能深入理解他们.本文从java doc触发,讲到它们与哈希表的关系,再到具体的实现,就我目前掌握 ...