UVA1204 Fun Game
Fun Game
https://odzkskevi.qnssl.com/8d698323a1e07d605cdeea708ee8a01d?v=1508703139
【题解】
不难发现如果一个串的原串或反转串是另一个串的子串,那么这个串是没有用的
我们把他剔除出去
如果此时只有一个串,暴力枚举解检查即可(网上很多写法是KMP。。不是很明白,没仔细看他们代码
多个串则状压DP
dp[s][i][0/1]表示s串已经选了,最后一个串是i,i是正着/倒着的,最大重叠字母数
刷表法转移即可
如何处理圈?我们强行在第一个串的地方断开,按照第一个串的正着的方向为圈的传递方向即可,
最后的时候枚举最后一个串跟第一个串交一下算答案
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b)) inline void swap(int &a, int &b)
{
long long tmp = a;a = b;b = tmp;
} inline void read(int& x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
} const int INF = 0x3f3f3f3f;
const int MAXN = ;
const int MAXNUM = ; int dp[( << MAXN) + ][MAXN + ][], die[MAXN][][MAXN][], n, ma, sum, len[MAXN], cnt[MAXN], tot, b[MAXN];
char s[MAXN][MAXNUM]; inline void init()
{
memset(dp, , sizeof(dp));
memset(die, , sizeof(die));
tot = ;
sum = ;
memset(b, , sizeof(b));
memset(len, , sizeof(len));
memset(cnt, , sizeof(cnt));
//check j是否包含在i中
for(register int i = ;i <= n;++i)
{
scanf("%s", s[i] + );
len[i] = strlen(s[i] + );
}
for(register int i = ;i <= n;++ i)
for(register int j = ;j <= n;++ j)
{
if(i == j || b[i] || b[j] || len[j] > len[i])continue;
//正着配
for(register int a = ;a <= len[i] - len[j] + ;++ a)
{
int tmp1 = a;
while(s[i][tmp1] == s[j][tmp1 - a + ] && tmp1 - a + <= len[j]) ++ tmp1;
if(tmp1 - a + > len[j]) b[j] = ;
}
//倒着配
for(register int a = len[i];a >= len[j];-- a)
{
int tmp1 = a, p = ;
while(s[i][tmp1] == s[j][p] && p <= len[j]) -- tmp1, ++ p;
if(p > len[j]) b[j] = ;
}
}
for(register int i = ;i <= n;++ i)
if(!b[i])
cnt[++ tot] = i, sum += len[i];
//j跟在i后面重叠部分大小
for(register int p = ;p <= tot;++ p)
for(register int q = ;q <= tot;++ q)
{
int i = cnt[p], j = cnt[q];
if(i == j)continue;
//0 0
for(register int a = ;a <= len[i];++ a)
{
int b = , tmp = a;
while(s[i][tmp] == s[j][b] && tmp <= len[i] && b <= len[j])++ tmp, ++ b;
if(tmp > len[i])
{
die[p][][q][] = len[i] - a + ;
break;
}
}
//0 1
for(register int a = ;a <= len[i];++ a)
{
int b = len[j], tmp = a;
while(s[i][tmp] == s[j][b] && tmp <= len[i] && b >= )++ tmp, -- b;
if(tmp > len[i])
{
die[p][][q][] = len[i] - a + ;
break;
}
}
//1 0
for(register int a = len[i];a >= ;-- a)
{
int b = , tmp = a;
while(s[i][tmp] == s[j][b] && tmp >= && b <= len[j])-- tmp, ++ b;
if(tmp < )
{
die[p][][q][] = a;
break;
}
}
//1 1
for(register int a = len[i];a >= ;-- a)
{
int b = len[j], tmp = a;
while(s[i][tmp] == s[j][b] && tmp >= && b >= )-- tmp, -- b;
if(tmp < )
{
die[p][][q][] = a;
break;
}
}
}
ma = << tot;
} int main()
{
while(scanf("%d", &n) != EOF && n)
{
init();
int flag = ;
if(tot == )
{
int x = cnt[tot];
for(register int i = ;i <= len[x];++ i)
{
for(register int j = ;j <= len[x];++ j)
{
if(j + i - > len[x]) break;
int p1 = , p2 = j, rank = ;
while(s[x][p1] == s[x][p2] && rank < len[x])
{
++ p1, ++ p2, ++ rank;
if(p1 > len[x]) p1 = ;
if(p2 > j + i - ) p2 = ;
}
if(rank >= len[x])
{
printf("%d\n", max(, i));
flag = ;
break;
}
}
if(flag) break;
}
if(flag) continue;
}
//dp[S][i][0/1]表示以1号字符串为头,已经选了S,最后一个是i的正/反状态的最大折叠数
//dp[S | k][k][p] = max(dp[S | k][k][p], dp[S][i][p'] + die[i][p'][k][p])
for(register int S = ;S < ma;++ S)
for(register int i = ;i <= tot;++ i)/*分别用dp[S][i][0]和dp[S][i][1]去更新*/
{
if(!(S & ))continue;
if(!(S & ( << (i - )))) continue;
for(register int k = ;k <= tot;++ k)
{
if(S & ( << (k - ))) continue;
if(S == )
{
dp[S | ( << (k - ))][k][] = max(dp[S | ( << (k - ))][k][], dp[S][i][] + die[i][][k][]);
dp[S | ( << (k - ))][k][] = max(dp[S | ( << (k - ))][k][], dp[S][i][] + die[i][][k][]);
}
else
{
dp[S | ( << (k - ))][k][] = max(dp[S | ( << (k - ))][k][], max(dp[S][i][] + die[i][][k][], dp[S][i][] + die[i][][k][]));
dp[S | ( << (k - ))][k][] = max(dp[S | ( << (k - ))][k][], max(dp[S][i][] + die[i][][k][], dp[S][i][] + die[i][][k][]));
}
}
}
int ans = ;
for(register int i = ;i <= tot;++i)
ans = max(ans, max(dp[ma - ][i][] + die[i][][][], dp[ma - ][i][] + die[i][][][]));
printf("%d\n", max(, sum - ans));
}
return ;
}
UVA1204
UVA1204 Fun Game的更多相关文章
随机推荐
- 给自己立下一个flag先
恩,今天开始写博客. 其实主要原因是被人甩了,想找个事情让自己忙起来. 主要原因是“男人没钱就是无能”,我是个应届毕业生.所以你懂的. 我不喜欢让心情不爽,所以只能找事情让自己忙起来.所以我开始立Fl ...
- codeforces 1100E-Andrew and Taxi
传送门:QAQQAQ 题意:给你一个图,每条边有边权,现在你可以对边进行反转,使图中不存在环,你需要使得反转的边的边权集合中的最大值最小,并输出任意一组解. 思路:二分+拓扑排序 使得最大值最小,自然 ...
- linux socket error code
errno.00 is: Successerrno.01 is: Operation not permittederrno.02 is: No such file or directoryerrno. ...
- 第一周课堂笔记3th
1.bool值 True正确 False错误 数字为0时的显示 为空值时“” 2. str int bool布尔值 之间的转化 str int ...
- InceptionV3代码解析
InceptionV3代码解析 参考博文:https://blog.csdn.net/superman_xxx/article/details/65451916 读了Google的GoogleNet以 ...
- Apache SOLR and Carrot2集成
1.环境 下载软件 名称 地址 solr-integration-strategies-gh-pages.zip https://github.com/carrot2/solr-integration ...
- utils05_git在idea下的操作
1.idea下将工程添加到本地仓库 1>找到自己的git.exe 2>创建本地的git仓库,将项目放入本地仓库 3> *从本地仓库更新 *提交到本地仓库 *比较版本差异 *丢弃我的修 ...
- 原生微信小程序数据渲染
一直在写vue,第一次接触微信小程序,还是原生,最开始做的时候真的很闹心啊啊啊啊啊啊啊啊啊啊啊啊!!所以最近大概更新的都是微信小程序原生的内容了~~么么哒!!一定会继续努力的!!tips:在小程序项目 ...
- MUI使用vue示例
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- 转:IO模型-- 同步和阻塞,异步和非阻塞的区别
源地址 http://hi.baidu.com/deep_pro/item/db0c581af1c1f17e7b5f2534 这些词之间的区别难倒了很多人,还有什么同步阻塞, 同步非阻塞, 异步阻塞, ...