Best Sequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6338   Accepted: 2461

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<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int nxt[],add[][];
int dp[(<<)+][];
char str[][];
void getNext(char *a,int len)
{
int i=,j=-;
nxt[]=-;
while(i<len)
{
if(j==-||a[i]==a[j]) nxt[++i]=++j;
else j=nxt[j]; }
}
int kmp(char *a,char *b,int l1,int l2)
{
int i=,j=;
while(i<l1)
{
if(j==-||a[i]==b[j]) ++i,++j;
else j=nxt[j];
}
return l2-j;
}
int main()
{
int n,T;
for(scanf("%d",&T); T--;)
{
scanf("%d",&n);
for(int i=; i<n; ++i) scanf("%s",str[i]);
memset(dp,INF,sizeof(dp));
for(int i=; i<n; ++i) dp[(<<i)][i]=strlen(str[i]);
for(int i=; i<n; ++i) for(int j=; j<n; ++j)
{
getNext(str[j],strlen(str[j]));
add[i][j]=kmp(str[i],str[j],strlen(str[i]),strlen(str[j]));
}
for(int i=;i<(<<n);++i) for(int j=;j<n;++j) if(dp[i][j]==INF) continue;
else for(int k=;k<n;++k) {
if((<<k)&i) continue;
dp[i|(<<k)][k]=min(dp[(<<k)|i][k],dp[i][j]+add[j][k]);
}
int ans=INF;
for(int i=;i<n;++i) ans=min(ans,dp[(<<n)-][i]);
printf("%d\n",ans);
}
}

最新文章

  1. 【腾讯优测干货分享】如何降低App的待机内存(四)——进阶:内存原理
  2. Python的安装和详细配置
  3. 快速删除.svn文件夹
  4. Mini ORM——PetaPoco笔记(转)
  5. 攻城狮在路上(肆)How tomcat works(二) 一个简单的servlet容器
  6. CentOS搭建LNMP环境
  7. Java调用CMD命令
  8. Json遇到引号需要转义的问题
  9. codeforces 609F. Frogs and mosquitoes 二分+线段树
  10. JavaScript命名规范基础及系统注意事项
  11. 笔记《JavaScript 权威指南》(第6版) 分条知识点概要1—词法结构
  12. Container and Injection in Java
  13. kubernetes 创建系统用户来支持访问 dashboard
  14. 纯CSS选项卡
  15. $Matrix-Tree$定理-题目
  16. 添加linux系统调用的两种方式
  17. 【转载】 强化学习(三)用动态规划(DP)求解
  18. XSS Challenges练习及解答
  19. Mac下配置Apache Httpd的Https/SSL
  20. arch/manjaro linux configuration

热门文章

  1. 预测球队比赛结果及利用pyinstaller打包文件
  2. CodeForces - 260B
  3. python(文件操作)
  4. CRT 连接AWS-EC2
  5. Python监控文件夹 &amp;&amp; 发送邮件
  6. js 如何保存代码段并执行以及动态加载script
  7. 使用npm发布插件
  8. (3).mybatis编写工具类
  9. mybatis添加信息自动生成主键
  10. Leetcode_45. 跳跃游戏 II