poj1699(状态压缩dp)
可能没有完全读懂题意。
个人觉得
acca
aa
答案应该是4.
然后就是dp了。。这题数据量小很多方法都可以,数据也水暴力据说都能过。。
还有就是我竟然没有用扩展kmp优化下。。。 太无耻了,我是因为找扩展kmp的题才来看这题的。
|
Best Sequence
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 Sample Output 11 Source |
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#include <sstream>
#include <iostream>
using namespace std;
#define INF 0x3fffffff int n;
char save[][];
char g[][];
int mark[];
int dp[][][];//状态压缩
int top=;
int sum[][]; int check(char s[],char t[])
{
int len=strlen(s);
int len1=strlen(t);
int cnt=;
int flag=;
for(int i=;i<len1;i++)
{
int tmp=i;
while(tmp<len1&&s[cnt]==t[tmp])
{
cnt++;
tmp++;
if(cnt==len)
{
flag=;
break;
}
}
cnt=;
}
return flag;
} int main()
{
//freopen("//home//chen//Desktop//ACM//in.text","r",stdin);
//freopen("//home//chen//Desktop//ACM//out.text","w",stdout);
int T;
scanf("%d",&T);
while(T--)
{
memset(mark,,sizeof(mark));
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%s",save[i]);
//////////////
for(int i=;i<n;i++)
{
for(int j=;j<n;j++)
{
if(i==j) continue;
if(mark[j]==&&check(save[i],save[j])==)
{
mark[i]=;
break;
}
}
}
top=; for(int i=;i<n;i++)
{
if(mark[i]==)
{
strcpy(g[top],save[i]);
top++;
}
} //////////////////////////////先做个预处理比较方便吧
memset(sum,,sizeof(sum)); for(int i=;i<top;i++)
for(int j=;j<top;j++)
{
if(i==j) continue;
int len=strlen(g[i]);
int len1=strlen(g[j]);
for(int k=min(len,len1)-;k>=;k--)
{
int flag=;
for(int i1=;i1<k;i1++)
if(g[i][len-k+i1]!=g[j][i1])
{
flag=;
break;
}
if(flag==)
{
sum[i][j]=len1-k;
break;
}
else sum[i][j]=len1;
}
} //这样就求出sum了
////////////////然后剩下的就是不会互相影响的了 for(int ii=;ii<=;ii++)
for(int i=;i<=;i++)
for(int j=;j<;j++)
dp[ii][i][j]=INF; for(int i=;i<top;i++)
{
int len=strlen(g[i]);
dp[][i][ (<<i) ] = len;
} for(int ii=;ii<top;ii++)
{
for(int j=;j<top;j++)
{
for(int k=;k<(<<top);k++)
{
if(dp[ii-][j][k]==INF) continue;
for(int i=;i<top;i++)
{
if( ( k&(<<i) )== )
{
//在之前没有出现过的时候..
dp[ii][i][(k|(<<i))]=min(dp[ii][i][( k|(<<i) )],dp[ii-][j][k]+sum[j][i]);
}
}
}
}
} int mi=INF;
for(int i=;i<top;i++)
for(int j=;j<(<<top);j++)
mi=min(dp[top-][i][j],mi);
printf("%d\n",mi);
}
return ;
}
poj1699(状态压缩dp)的更多相关文章
- hoj2662 状态压缩dp
Pieces Assignment My Tags (Edit) Source : zhouguyue Time limit : 1 sec Memory limit : 64 M S ...
- POJ 3254 Corn Fields(状态压缩DP)
Corn Fields Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 4739 Accepted: 2506 Descr ...
- [知识点]状态压缩DP
// 此博文为迁移而来,写于2015年7月15日,不代表本人现在的观点与看法.原始地址:http://blog.sina.com.cn/s/blog_6022c4720102w6jf.html 1.前 ...
- HDU-4529 郑厂长系列故事——N骑士问题 状态压缩DP
题意:给定一个合法的八皇后棋盘,现在给定1-10个骑士,问这些骑士不能够相互攻击的拜访方式有多少种. 分析:一开始想着搜索写,发现该题和八皇后不同,八皇后每一行只能够摆放一个棋子,因此搜索收敛的很快, ...
- DP大作战—状态压缩dp
题目描述 阿姆斯特朗回旋加速式阿姆斯特朗炮是一种非常厉害的武器,这种武器可以毁灭自身同行同列两个单位范围内的所有其他单位(其实就是十字型),听起来比红警里面的法国巨炮可是厉害多了.现在,零崎要在地图上 ...
- 状态压缩dp问题
问题:Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Ev ...
- BZOJ-1226 学校食堂Dining 状态压缩DP
1226: [SDOI2009]学校食堂Dining Time Limit: 10 Sec Memory Limit: 259 MB Submit: 588 Solved: 360 [Submit][ ...
- Marriage Ceremonies(状态压缩dp)
Marriage Ceremonies Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%lld & %llu ...
- HDU 1074 (状态压缩DP)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1074 题目大意:有N个作业(N<=15),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- 云、Iaas、Paas、Saas
云服务现在已经快成了一个家喻户晓的词了.如果你不知道PaaS, IaaS 和SaaS的区别,那么也没啥,因为很多人确实不知道. 云其实是互联网的一个隐喻,云计算其实就是使用互联网来接入存储或者运行在远 ...
- UE添加鼠标右键打开
重装了下系统,有一些工具,不用重新安装还是能用的,比如UE.pl/sql,就当是绿色软件了.但是有些在鼠标右键里的功能没有了 ,比如UE的右键打开. 可以这样修改:打开UltraEdit->高级 ...
- glassfish3操作命令
glassfish3操作命令 启动:C:\Java\glassfish\bin>asadmin start-domain domain1 停止:C:\Java\glassfish\bin&g ...
- 【转】ESFramework成熟的C#网络通信框架(跨平台)
原文地址:http://www.cnblogs.com/zhuweisky/archive/2010/08/12/1798211.html ESFramework网络通信框架是一套性能卓越.稳定可靠. ...
- VMware安装Centos7超详细过程(图文)
1.软硬件准备 软件:推荐使用VMwear,我用的是VMwear 12 镜像:CentOS6 ,如果没有镜像可以在阿里云下载 https://mirrors.aliyun.com/centos/ 硬件 ...
- Oracle Report(Report 6i/RDF)使用全局变量fnd_global, fnd_frofile
注:本文为原创,作为学习交流使用,转载请标明作者及出处,作者保留追究法律责任的权力. Lumen Su lzsu1989#gmail.com (#=@) www.cnblogs.com/lzsu198 ...
- Android_JarZip压缩和解压文件
本文资料来自<android开发权威指南> AndroidSDK中提供了java.util.jar和java.util.zip包中的若干类和接口来完成. 压缩文件基本步骤: 1.创 ...
- 从12306网站新验证码看Web验证码设计与破解
2015年3月16日,铁路官方购票网站12306又出新招,在登录界面推出了全新的验证方式,用户在填写好登录名和密码之后,还要准确的选取图片验证码才能登陆成功.据悉,12306验证码改版后,目前所有抢票 ...
- 【Android界面实现】Drawable Animation 使用介绍
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992 (眼下仅仅能用在View对象上的动画效果的实现有两种,一种就是上一篇的View Animat ...
- Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理
Atitit. 注册表操作查询 修改 api与工具总结 java c# php js python 病毒木马的原理 1. reg 工具 这个cli工具接口有,优先使用,jreg的要调用dll了,麻烦的 ...
