您的位置:首页 > 其它

Uva 12012 Detection of Extraterrestrial 解题报告

2013-05-18 21:56 399 查看
题意:子串循环k次能构成的新的子串长度最长是多少

解法:枚举后缀,找出next数组,再枚举每个后缀的前缀,找出循环节

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 1005

int n;
char st[maxn], s[maxn];
int next[maxn], ans[maxn];

void getnext()
{
int i=0, j=-1;
next[0]=-1;
int l=strlen(st);
while (i<l)
{
if (j==-1||st[i]==st[j]) i++, j++, next[i]=j;
else j=next[j];
}
}

int main()
{
int cas, t=0;
scanf("%d", &cas);
while (cas--)
{
scanf("%s", s);
int n=strlen(s);
memset(ans, 0, sizeof(ans));
for (int i=0; i<n; i++)
{
for (int j=i; j<n; j++)
st[j-i]=s[j];
st[n-i]='\0';
getnext();
for (int j=1; j<=n-i; j++)
if (next[j] && j%(j-next[j])==0)
{
int tt=j/(j-next[j]);
for (int k=1; k<=tt; k++)
if (tt%k==0)
ans[k]=max(ans[k], j);
else ans[k]=max(ans[k], (j-next[j])*k);
}
}
ans[1]=n;
printf("Case #%d:", ++t);
for (int i=1; i<=n; i++)
printf(" %d", ans[i]);
printf("\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: