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),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- 前端纯css 图片的模糊处理
最近做的一个项目需要用到背景图的模糊处理,在网上查资料,发现用css的 filter 属性就可以解决,但是因为模糊度比较高,四周会有很长的模糊边,百度上也没找到解决的方法,,可喜的是最后我在分析一个d ...
- LaTeX 中插入图片使其紧跟插入的文字之后
LaTeX 中插入图片使其不跑到每页的开头而紧跟插入的文字之后. 此次建模过程中,遇到的一个比较棘手的问题是: 当插入图片时,图片的位置总是会自动跑到当页(或下一页)的最上方,而不是紧跟在其对应的说明 ...
- Python 使用 UTF-8 编码(转)
Python 使用 UTF-8 编码(转) 原文出处:http://blog.chenlb.com/2010/01/python-use-utf-8.html 一般我喜欢用 utf-8 编码,在 py ...
- ASP.NET 推荐书籍
ASP.NET 推荐书籍 1.首先推荐一本ASP.NET MVC的书籍 —— <Web开发新体验:ASP.NET 3.5 MVC架构与实战> [点评]:ASP.NET的MVC的书籍本来就不 ...
- unity3d对象的克隆
对象克隆,需要有一个预制对象. 首先创建一个预制对象,将其命名为earth,并将材质包附着上去,如下图所示: 然后点住“earth”拖入Assets>Prefabs文件夹,此时earth文字变成 ...
- RabbitMQ的应用场景以及基本原理介绍(转载)
1.背景 RabbitMQ是一个由erlang开发的AMQP(Advanved Message Queue)的开源实现. 2.应用场景 2.1异步处理 场景说明:用户注册后,需要发注册邮件和注册短信, ...
- 零样本学习 - (Zero shot learning,ZSL)
https://zhuanlan.zhihu.com/p/41846072 https://zhuanlan.zhihu.com/p/38418698 https://zhuanlan.zhihu.c ...
- 重置linux mysql root密码
2.修改MySQL的登录设置: # vi /etc/my.cnf 在[mysqld]的段中加上一句:skip-grant-tables 例如: [mysqld] datadir=/var/lib/my ...
- Smart Client技术简要总结
摘要:Smart Client简称智能客户端,是Microsoft推出的一种将B/S(瘦客户端)和C/S(胖客户端)结合在一起的一种技术.Smart Client结合了B/S和C/S的优势,具有新的特 ...
- 【ZT】NUMA架构的CPU -- 你真的用好了么?
本文从NUMA的介绍引出常见的NUMA使用中的陷阱,继而讨论对于NUMA系统的优化方法和一些值得关注的方向. 文章欢迎转载,但转载时请保留本段文字,并置于文章的顶部 作者:卢钧轶(cenalulu) ...
