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),每个作业需耗时,有一个截止期限.超期多少天就要扣多少 ...
随机推荐
- java通过CLASSPATH读取包内文件
读取包内文件,使用的路径一定是相对的classpath路径,比如a,位于包内,此时可以创建读取a的字节流:InputStream in = ReadFile.class.getResourceAsSt ...
- Vmware虚拟机三种网络模式详解(转)
原文来自http://note.youdao.com/share/web/file.html?id=236896997b6ffbaa8e0d92eacd13abbf&type=note 我怕链 ...
- mongodb - Replication Set成员维护
1.添加复制集成员 ybb:PRIMARY> rs.add("cc.example.com:27020") { "ok" : 1 } ybb:PRIMAR ...
- python-求直角三角形斜边
设计一个求直角三角形斜边长的函数(两条直角边为参数,求最长边) 如果直角边边长分分别为3和4,那么返回的结果应该像这样: The right triangle third side's length ...
- Java Jar maven 下载地址
Java Jar maven 下载地址 https://repo1.maven.org/maven2/ 根据maven artifactid 查找相关jar
- mysql general log 查看mysql 运行历史
我们有时候须要查看mysql的运行历史,比方我们做sql优化的时候,起码要知道运行的sql是什么.框架通常会帮我们拼装sql,所以在程序中不一定能够打印出sql,这个时候就须要mysql的genera ...
- NGUI ScrollView中的Bounds
获取到的Bounds值是固定的,是因为Bounds区域的计算是被动计算,需要主动调用使其刷新 scrollView.InvalidateBounds(); 另外Bounds的Min和Max似乎和NGU ...
- Jetty - Connector源码分析
1. 描述 基于Jetty-9.4.8.v20171121. Connector接受远程机器的连接和数据,允许应用向远程机器发送数据. 1.2 类图 从类图看出AbstractConnector继承C ...
- Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结
Atitit.angular.js 使用最佳实践 原理与常见问题解决与列表显示案例 attilax总结 1. 本文范围 1 2. Angular的优点 1 2.1. 双向数据绑定 1 2.2. dsl ...
- Git-git 忽略 IntelliJ .idea文件
$ echo ‘.idea’ >> .gitignore $ git rm -rf .idea $ git add .gitignore
