poj 1699 TSP(状态压缩DP + KMP)
来源:互联网 发布:共享网络无线发射器 编辑:程序博客网 时间:2024/04/29 21:42
题意:给你n个串,让你构造出一个最短的串,使得这n个串每个串都是这个串的子串,输出你构造的串的长度即可
(如果要输出构造的串中字典序最小的,就有点难了)
数据范围小,不一定要使用KMP预处理的
分两步:
1:预处理出一个串的前缀最多匹配多少长度的另一个串的后缀
(如果要输出构造的串中字典序最小的,就有点难了)
数据范围小,不一定要使用KMP预处理的
分两步:
1:预处理出一个串的前缀最多匹配多少长度的另一个串的后缀
2:然后就相当于旅行商问题了,每两个串之间都有一定的距离,设dp[i][j]表示i状态的字符串,最后一个字符串为j时所构造的字符串的最短长度,每次枚举一个中间点进行转移即可,具体见代码
这题用ac自动机死活过不了,蛋疼啊
#include <set>#include <map>#include <stack>#include <queue>#include <deque>#include <cmath>#include <vector>#include <string>#include <cstdio>#include <cstdlib>#include <cstring>#include <iostream>#include <algorithm>using namespace std;#define L(i) i<<1#define R(i) i<<1|1#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-9#define maxn 10010#define MOD 1000000007int n,m;char str[16][110];int len[20][20];int p[110];void getp(int m,char b[]){ p[1]=0; int i,j=0; for(i=2;i<=m;i++){ while(j>0&&b[j+1]!=b[i]) j=p[j]; if(b[j+1]==b[i]) j+=1; p[i]=j; }}int kmp(int n,int m,char a[],char b[]){ int i,j=0,Max=0; for(i=1;i<=n;i++){ while(j>0&&b[j+1]!=a[i]) j=p[j]; if(b[j+1]==a[i]) j+=1; Max=j; if(j==m){ j=p[j]; } } return Max;}int dp[1<<15][15];int pre[15];int le[20];int main(){ int t,ca=1,n; scanf("%d",&t); while(t--) { scanf("%d",&n); for(int i=0;i<n;i++) scanf("%s",str[i]+1),le[i]=strlen(str[i]+1); memset(len,0,sizeof(len)); for(int i=0;i<n;i++) { for(int j=i+1;j<n;j++) { memset(p,0,sizeof(p)); getp(le[i],str[i]); int tmp=kmp(le[j],le[i],str[j],str[i]); if(tmp>len[i][j]) len[i][j]=tmp; getp(le[j],str[j]); tmp=kmp(le[i],le[j],str[i],str[j]); if(tmp>len[j][i]) len[j][i]=tmp; } } memset(dp,INF,sizeof(dp)); for(int i = 0; i < n; i++) dp[1<<i][i] = le[i]; for(int i = 0; i < (1<<n); i++) for(int j = 0; j < n; j++) for(int k = 0; k < n; k++) { if(!(i&(1<<k)) || !(i&(1<<j)) || j == k) continue; dp[i|(1<<k)][k] = min(dp[i|(1<<k)][k],dp[i][j]+le[k]-len[k][j]); //dp[i][j] = min(dp[i][j],dp[i^(1<<j)][k]+le[j]-len[j][k]); } int ans = INF; for(int i = 0; i < n; i++) ans = min(ans,dp[(1<<n)-1][i]); printf("%d\n",ans); } return 0;}
0 0
- poj 1699 TSP(状态压缩DP) + KMP
- poj 1699 TSP(状态压缩DP + KMP)
- POJ 1699 Best Sequence (TSP之dp状态压缩+KMP计算主串前缀匹配子串后缀)
- poj 3229 简单 TSP 状态压缩dp
- poj 2688 状态压缩dp解tsp
- poj 2288(状态压缩dp + TSP问题)
- POJ3311(TSP问题,状态压缩DP)
- POJ 3311 Hie with the Pie -- TSP 状态压缩dp
- POJ-2288 Islands and Bridges 状态压缩DP TSP
- poj 3311 2进制状态压缩dp,tsp
- POJ 3311 Hie with the Pie (状态压缩DP ,tsp问题)
- POJ 3311 Hie with the Pie (状态压缩DP ,TSP问题)
- TSP状态转移[状态压缩DP]
- HDU-4568 Hunter 最短路 + 状态压缩DP(TSP)
- 三进制状态压缩DP(旅行商问题TSP)HDU3001
- 二进制状态压缩dp(旅行商TSP)POJ3311
- HDU3681Prison Break(BFS+TSP+二分+dp状态压缩)
- HDU 3001 Travelling (状态压缩DP +TSP问题)
- 详解spl_autoload_register()函数
- UVALive5429 UVA382 POJ1528 HDU1323 ZOJ1284 Perfection
- Asp.Net开通支付宝PC端网页支付
- android app 开启自动启动
- 在mysql中,以下哪种方式可以开启一个事务?
- poj 1699 TSP(状态压缩DP + KMP)
- I/O多路转接之select
- Linux命令:改变文件或目录的访问权限
- 清除xcode缓存
- Android安全机制探讨
- ubuntu一键安装禅道系统
- UICollectionViewLayout
- 面向过程与面向对象
- HTML图片热区map area的用法整理