【玲珑杯 1054】【暴力+枚举约数】String cut
2016-11-06 01:10
309 查看
传送门:http://www.ifrog.cc/acm/problem/1054
思路:
首先知道这样一个性质,删除一个字符之后的字符串的循环节长度一定是nn的约数。所有我们就枚举删除哪一个字符,然后对每一个约数判断是否可以循环节为它。复杂度O(nlogn)
代码:
描述:
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
思路:
首先知道这样一个性质,删除一个字符之后的字符串的循环节长度一定是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
相关文章推荐
- 玲珑杯”ACM比赛 Round #4 1054 - String cut 暴力。学到了扫描的另一种思想
- [poj1054] The Troublesome Frog 暴力枚举+剪支
- poj 1054 The Troublesome Frog 排序后暴力枚举,剪枝优化
- (POJ 1054)The Troublesome Frog <暴力枚举+剪枝 || DP>
- HDOJ-----5339---Untitled暴力枚举+剪枝
- CF#278 (Div. 2) A.(暴力枚举)
- UVA10010(八方向暴力枚举)
- HDU - 6152 Friend-Graph(规律/暴力枚举)
- hdoj5478Can you find it【暴力枚举+快速幂】
- hdu 5128 暴力枚举
- uvaoj 10717 Mint 暴力枚举所有情况
- 【3091】暴力枚举?
- HDU 3268/POJ 3835 Columbus’s bargain(最短路径+暴力枚举)(2009 Asia Ningbo Regional)
- HDU/HDOJ 1015(dfs或暴力枚举)
- uva 10167 Birthday Cake(暴力/枚举)
- 蓝桥杯--关于暴力枚举
- hihoCoder - 1094 - Lost in the City (暴力枚举!!)
- Bzoj 2241: [SDOI2011]打地鼠 暴力,枚举,贪心
- LightOJ - 1014 Ifter Party 暴力枚举因子
- CodeForces 724B(暴力枚举)