您的位置:首页 > 其它

【玲珑杯 1054】【暴力+枚举约数】String cut

2016-11-06 01:10 309 查看
传送门:http://www.ifrog.cc/acm/problem/1054

思路:

首先知道这样一个性质,删除一个字符之后的字符串的循环节长度一定是nn的约数。所有我们就枚举删除哪一个字符,然后对每一个约数判断是否可以循环节为它。复杂度O(nlogn)

代码:

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
using  namespace  std;

#define rep(i,k,n) for(int i=k;i<=n;i++)
#define rrep(i,k,n) for(int i=k;i>=n;i--)
#define pl(x) cout << #x << "= " << x << endl;

template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
const int N=1e5+10;
string s;

int  main(){
int T;
read(T);
while(T--){
int n;
read(n);
// if(n==1) return 0*puts(0);
cin>>s;
int mx=0;
rep(i, 0, n-1){
string ss = s.substr(0, i);
ss = ss+s.substr(i+1, n-i);
// cout<<"ss="<<ss<<endl;
// pl(i);
for(int d = 1; d<=n-1; d++){
if((n-1)%d==0)
{//这个条件不能乱放到循环里面
if((n-1)/d<=mx)break;
bool flag = true;
string sub = ss.substr(0, d);
for(int j=0; j<=n-2; j+=d)
{
string sb = ss.substr(j, d);
// pl(sb);
if(sb != sub)
{
flag=false;
break;
}
}
if(flag)
{
mx = max(mx, (n-1)/d);
}
}
}
}
printf("%d\n", mx);
}
return 0;
}


描述:

1054 - String cut

Time Limit:4s Memory Limit:64MByte

Submissions:151Solved:54

DESCRIPTION

A string cut cc
means the string can be divided into
cc
same substring. Such as string:
abababababab.Because
it can be divied into abab
+ abab
+ abab,
the string cut of it is 33.

Give you a string strstr.you
need to delete one char of it ,and after your operation the string cut of it will be biggest.Print the biggest string cut.

INPUT

There are multiple test cases.The first line is a number T (T ≤10T ≤10),
which means the number of cases.For each case, a line has a integer
n(1<=n<=100001)n(1<=n<=100001),which
is the length of string.next line a string
str str (which
just include lowercase).

OUTPUT

one line --- the biggest string cut.

SAMPLE INPUT

23aab5aabaa

SAMPLE OUTPUT

24

SOLUTION

“玲珑杯”ACM比赛 Round #4
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: