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