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. java stream中Collectors的用法

    目录 简介 Collectors.toList() Collectors.toSet() Collectors.toCollection() Collectors.toMap() Collectors ...

  2. muduo网络库源码学习————线程类

    muduo库里面的线程类是使用基于对象的编程思想,源码目录为muduo/base,如下所示: 线程类头文件: // Use of this source code is governed by a B ...

  3. python——remove,del,pop三种删除元素方式的区别

    记性不好,整理出来以作保存 1.remove ①直接删除元素,remove(obj),顺序删除第一个遇到的,所以想要全部删除 ,需要遍历 aList = [123, 'xyz', 'zara', 'a ...

  4. 小白,你要的Java抽象类,操碎了心!

    自从给小白写了两篇科普性质的文章后,我就有点一发不可收拾,觉得很有必要继续写下去.因为有读者留言"鼓励"我说,"二哥,你真的是为小白操碎了心啊!"我容易吗?我. ...

  5. libevent(六)http server

    客户端: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <signa ...

  6. 2-MyBatisPlus教程(HelloWorld)

    1,准备数据 DROP TABLE IF EXISTS user; CREATE TABLE user ( id ) NOT NULL COMMENT '主键ID', name ) NULL DEFA ...

  7. POJ2376Cleaning Shifts(区间覆盖贪心)

    应该还是蛮简单的一题,但是因为模拟太差,一直没调出来....... \(显而易见的应该按照左区间从小到大排序,相等按照右区间大到小排序\). \(那么第一个区间的l一定要是1,而且必拿(否则没有区间能 ...

  8. POJ3279(开关后续)

    描述: 一个\(n*m的矩阵,每个格子有0和1两种状态.每次可以翻一个格子,并且此格子的上下左右都要被翻.\) \(目标状态应该全为0,求最少翻的次数,输出最小字典序的方案\) 这儿可就麻烦了啊,开关 ...

  9. P1666前缀单词

    题目传送门点我传送 Ⅰ.字典树+树型DP 非常奇妙的一种解法 第一部分:构建树 先对来的单词读入,插入字典树 然后对于一颗字典树,其实是有很多无用边的,所以我们需要删去一些边 删去非单词节点和非单词节 ...

  10. LeetCode--Unique Morse Code Words && Flipping an Image (Easy)

    804. Unique Morse Code Words (Easy)# International Morse Code defines a standard encoding where each ...