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);
}
}

poj1699 KMP+壮压DP的更多相关文章

  1. [Usaco2006 Nov]Corn Fields牧场的安排 壮压DP

    看到第一眼就发觉是壮压DP 然后就三进制枚举子集吧. 这题真是壮压入门好题... 对于dp[i][j] 表示第i行,j状态下前i行的分配方案数. 那么dp[i][j]肯定是从i-1行转过来的 那么由于 ...

  2. POJ 2686 Traveling by Stagecoach 壮压DP

    大意是有一个人从某个城市要到另一个城市(点数<=30) 然后有n个马车票,相邻的两个城市走的话要消耗掉一个马车票. 花费的时间呢,是马车票上有个速率值,用边/速率就是花的时间. 问最后这个人花费 ...

  3. 【BZOJ4560】[JLoi2016]字符串覆盖 KMP+状压DP

    [BZOJ4560][JLoi2016]字符串覆盖 Description 字符串A有N个子串B1,B2,…,Bn.如果将这n个子串分别放在恰好一个它在A中出现的位置上(子串之间可以重叠)这样A中的若 ...

  4. hdu 4284 Travel(壮压DP&TSP&floyd)

    Travel Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  5. hdu 3006 The Number of set(思维+壮压DP)

    The Number of set Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others ...

  6. uvalive 6669 hidden tree(好壮压dp)

    题目见option=com_onlinejudge&Itemid=8&page=show_problem&problem=4681">here 题意:给一个序列 ...

  7. 【hdu3247-Resource Archiver】位压DP+AC自动机+SPFA

    题意:给定n个文本串,m个病毒串,文本串重叠部分可以合并,但合并后不能含有病毒串,问所有文本串合并后最短多长. (2 <= n <= 10, 1 <= m <= 1000) 题 ...

  8. FJNU Fang G and his Friends(状压DP)题解

    Description     众所周知,fang G 有很多小伙伴,有一天,Fang G 打算带他们去玩有趣的游戏OOXX,这个游戏需要分成两组,有趣的是,每个人互相之间都有一个满意度,大家都想和自 ...

  9. Islands and Bridges(POJ2288+状压dp+Hamilton 回路)

    题目链接:http://poj.org/problem?id=2288 题目: 题意:求Hamilton 路径权值的最大值,且求出有多少条权值这么大的Hamilton路径. 思路:状压dp,dp[i] ...

随机推荐

  1. jenkins及Maven介绍

    一.环境介绍 随着软件开发需求及复杂度的不断提高,团队开发成员之间如何更好地协同工作以确保软件开发的质量已经慢慢成为开发过程中不可回避的问题.Jenkins自动化部署可以解决集成.测试.部署等重复性的 ...

  2. Tomcat7 启动慢的问题解决

    [问题] 由于上面标记部分,导致启动耗时将近160s,不能忍! [原因] 此于jvm环境配置有关,请打开jvm安装目录中jre/lib/security/java.security文件,找到secur ...

  3. CentOS7编译安装NodeJS

    概述 在CentOS7下采用编译NodeJS二进制源码包的方式安装NodeJS 下载NodeJS安装包 你可以先下载NodeJS二进制源码安装包文件然后上传到CentOS系统,也可以通过wget命令直 ...

  4. 百度云BaaS体系揭秘,突破共识机制、单机计算和串行处理三大瓶颈

    区块链作为去中心化的技术机制拥有广泛的应用场景与市场潜能.自2017年爆发式增长后,区块链虽然已经进入平稳期,但仍然存在概念混淆.技术性能制约.智能合约制约.共识机制.网络建设等痛点.为了打破行业壁垒 ...

  5. 我对sessionid的理解

    不知道是不是扯蛋,还是太菜... 看上面的的话毫不关系是吧...自己看过一点 关于 说session id 的 一些文章, 貌似都是一样的....以下内容个人理解, 请大家指正... 我想自己举个例子 ...

  6. 洛谷P3018 [USACO11MAR]树装饰Tree Decoration

    洛谷P3018 [USACO11MAR]树装饰Tree Decoration树形DP 因为要求最小,我们就贪心地用每个子树中的最小cost来支付就行了 #include <bits/stdc++ ...

  7. Openstack HA集群5-Keystone HA

    # yum install -y openstack-keystone httpd mod_wsgi # mysql -u root -p -e "CREATE DATABASE keyst ...

  8. 网课应该这么刷(油猴Tampermonkey脚本自动刷课)

    懒人福利 首先有些人不想学怎么用脚本,满足你们,压缩包解压之后直接登录即可.戳我下载 脚本已经集成好了,登录即可刷课.章节测试还会自动答题呦,正确率高达97%呦. 油猴及脚本安装 油猴的脚本不知可以刷 ...

  9. 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)

    Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...

  10. Python基础02 变量

    Python中的变量有两个特点: 1. 无需声明 a = 1 2. 不与类型绑定 a = 1 a = 'hello world' 变量名只是内存中具体对象的一个引用(reference). 对于 a ...