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. 配置路由器/交换机的Telnet登录

    实验目的:给配置路由器/交换机管理IP地址.设置Telnet的登录帐号.密码. 第一步:配置路由器的名称.接口IP地址. Switch> Switch>en Switch# Switch# ...

  2. Nginx入门及如何反向代理解决生产环境跨域问题

    1.Nginx入门与基本操作篇 注:由于服务器是windows系统,所以本文主要讲解Nginx在windows下的操作. 首先下载Nginx 解压缩,我们所有的配置基本都在万能的 nginx/conf ...

  3. MYSQl 全表扫描以及查询性能

    MYSQl 全表扫描以及查询性能 -- 本文章仅用于学习,记录 一. Mysql在一些情况下全表检索比索引查询更快: 1.表格数据很少,使用全表检索会比使用索引检索更快.一般当表格总数据小于10行并且 ...

  4. 最短路径树:Dijstra算法

    一.背景 全文根据<算法-第四版>,Dijkstra算法.我们把问题抽象为2步:1.数据结构抽象   2.实现 二.算法分析 2.1 数据结构 顶点+边->图.注意:Dijkstra ...

  5. 【学习笔记】Shell-1 变量:命名规范、变量赋值/取值/取消、局部变量/全局变量、预设环境变量

    1.Shell变量 从变量的实质上来说,变量名是指向一片用于存储数据的内存空间. Shell变量是一种弱类型的变量,即声明变量时不需要指定其变量类型,也不需求遵循“先声明再使用”的规定,想用即可用. ...

  6. 001_python变量,if,while

    Python介绍 python的出生与应用 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆(中文名字:龟叔)为了在阿姆斯特丹打发时间, ...

  7. Python网络爬虫练习

    1. 豆瓣top250电影 1.1 查看网页 目标网址:https://movie.douban.com/top250?start=0&filter= start=后面的数字从0,25,50一 ...

  8. 【Hadoop离线基础总结】日志采集框架Flume

    日志采集框架Flume Flume介绍 概述 Flume是一个分布式.可靠.和高可用的海量日志采集.聚合和传输的系统.它可以采集文件,socket数据包.文件.文件夹.kafka等各种形式源数据,又可 ...

  9. 【Hadoop离线基础总结】MapReduce入门

    MapReduce入门 Mapreduce思想 概述 MapReduce的思想核心是分而治之,适用于大量复杂的任务处理场景(大规模数据处理场景). 最主要的特点就是把一个大的问题,划分成很多小的子问题 ...

  10. dumpsys-package

    dumpsys-package ams和pms是android系统最重要的系统服务,本文解析dumpsys package命令,看哪些PMS相关的系统信息,数据结构是运行时可以查看的. 命令提示 co ...