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的更多相关文章
随机推荐
- 逻辑回归代码demo
程序所用文件:https://files.cnblogs.com/files/henuliulei/%E5%9B%9E%E5%BD%92%E5%88%86%E7%B1%BB%E6%95%B0%E6%8 ...
- R语言数据预处理
R语言数据预处理 一.日期时间.字符串的处理 日期 Date: 日期类,年与日 POSIXct: 日期时间类,精确到秒,用数字表示 POSIXlt: 日期时间类,精确到秒,用列表表示 Sys.date ...
- JS对象迭代v-for
<!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...
- Linux下c开发 之 线程通信
Linux下c开发 之 线程通信 1.Linux“线程” 进程与线程之间是有区别的,不过Linux内核只提供了轻量进程的支持,未实现线程模型.Linux是一种“多进程单线程”的操作系统.Linux本身 ...
- PAT甲级——A1088 Rational Arithmetic
For two rational numbers, your task is to implement the basic arithmetics, that is, to calculate the ...
- varStatus实现<c:forEach获取元素的下标 集合长度
有时候需要获取元素的下标,来实现一个编号的效果 那么使用 varStatus可以实现 代码 效果 如果需要把序号倒着数 那就用集合的长度减去当前下标 首先引入 <%@ taglib prefix ...
- INI文件读写类
public class INIClass { public string inipath; [DllImport("kernel32")] private static exte ...
- wpf中数据绑定和INotifyPeropertyChanged的理解
原创:转载请注明出处. 先说数据绑定: XAML代码: <Window x:Class="数据绑定和INotifyPropertyChanged.Window1" Loade ...
- Luogu P3558 [POI2013]BAJ-Bytecomputer(线性dp)
P3558 [POI2013]BAJ-Bytecomputer 题意 给一个只包含\(-1,0,1\)的数列,每次操作可以让a[i]+=a[i-1],求最少操作次数使得序列单调不降.若无解则输出BRA ...
- Python之MySQL语法(增删改查)
-- ID: 新闻的唯一标示 -- title: 新闻的标题 -- content: 新闻的内容 -- created_at: 新闻添加的时间 -- types: 新闻的类型 -- image: 新的 ...