http://poj.org/problem?id=2406

题意:给定一个字符串 L,已知这个字符串是由某个字符串 S 重复 R 次而得到的,求 R 的最大值。(长度<=1000000)

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;
int p[2000000];
char s[2000000];
int main() {
while(scanf("%s", s+1), s[1]!='.') {
int j=0, n=strlen(s+1); p[1]=0;
for(int i=2; i<=n; ++i) {
while(j && s[j+1]!=s[i]) j=p[j];
if(s[j+1]==s[i]) ++j;
p[i]=j;
}
if(n%(n-p[n])==0) printf("%d\n", n/(n-p[n]));
else puts("1");
}
return 0;
}

kmp求出next后那么最短循环串的长度为n-next[n],只需要判断n是否整除它即可。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std; const int N=2000005;
inline void sort(int *x, int *y, int *sa, int n, int m) {
static int c[N], i;
for(i=0; i<m; ++i) c[i]=0;
for(i=0; i<n; ++i) ++c[x[y[i]]];
for(i=1; i<m; ++i) c[i]+=c[i-1];
for(i=n-1; i>=0; --i) sa[--c[x[y[i]]]]=y[i];
}
inline void hz(int *a, int *sa, int n, int m) {
static int t1[N], t2[N], i, j, p, *x, *y, *t;
x=t1, y=t2;
for(i=0; i<n; ++i) x[i]=a[i], y[i]=i;
sort(x, y, sa, n, m);
for(j=1, p=1; p<n; j<<=1, m=p) {
p=0;
for(i=n-j; i<n; ++i) y[p++]=i;
for(i=0; i<n; ++i) if(sa[i]-j>=0) y[p++]=sa[i]-j;
sort(x, y, sa, n, m);
for(t=x, x=y, y=t, p=1, x[sa[0]]=0, i=1; i<n; ++i)
x[sa[i]]=y[sa[i]]==y[sa[i-1]]&&y[sa[i]+j]==y[sa[i-1]+j]?p-1:p++;
}
}
inline void geth(int *s, int *sa, int *rank, int *h, int n) {
static int j, i, k;
for(i=1; i<=n; ++i) rank[sa[i]]=i;
for(k=0, i=1; i<=n; h[rank[i++]]=k)
for(k?--k:0, j=sa[rank[i]-1]; s[i+k]==s[j+k]; ++k);
} int a[N], sa[N], h[N], rank[N], n;
char s[N];
inline int work() {
static int len[N], pos;
pos=rank[1];
for(int i=pos, mn=h[i]; i>1; --i) mn=min(h[i], mn), len[i-1]=mn;
for(int i=pos+1, mn=h[i]; i<=n; ++i) mn=min(h[i], mn), len[i]=mn;
int sq=sqrt(n+0.5);
for(int k=1; k<=sq; ++k) if(n%k==0 && len[rank[k+1]]==n-k) return n/k;
return 1;
}
int main() {
while(scanf("%s", s+1), s[1]!='.') {
n=strlen(s+1);
for(int i=1; i<=n; ++i) a[i]=s[i];
a[0]=0;
hz(a, sa, n+1, 255);
geth(a, sa, rank, h, n);
printf("%d\n", work());
}
return 0;
}

  


经典题...可是我tle了......因为本题听说是用kmp.............QAQ

sa的做法就是,求出height后,我们只匹配suffix(1)和suffix(k+1)的最长公共前缀是否为n-k即可,k是枚举的长度...至于为什么,请自己想...

【POJ】2406 Power Strings的更多相关文章

  1. POJ——T 2406 Power Strings

    http://poj.org/problem?id=2406 Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 50627   ...

  2. 【POJ】3134 Power Calculus

    1. 题目描述给定一个正整数$n$,求经过多少次乘法或除法运算可以从$x$得到$x^n$?中间结果也是可以复用的. 2. 基本思路实际结果其实非常小,肯定不会超过20.因此,可以采用IDA*算法.注意 ...

  3. poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)

    http://poj.org/problem?id=2406 Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submiss ...

  4. poj 2406 Power Strings 后缀数组解法

    连续重复子串问题 poj 2406 Power Strings http://poj.org/problem?id=2406 问一个串能否写成a^n次方这种形式. 虽然这题用kmp做比较合适,但是我们 ...

  5. KMP POJ 2406 Power Strings

    题目传送门 /* 题意:一个串有字串重复n次产生,求最大的n KMP:nex[]的性质应用,感觉对nex加深了理解 */ /************************************** ...

  6. POJ 2406 Power Strings (KMP)

    Power Strings Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 29663Accepted: 12387 Descrip ...

  7. 【POJ】1704 Georgia and Bob(Staircase Nim)

    Description Georgia and Bob decide to play a self-invented game. They draw a row of grids on paper, ...

  8. 【POJ】1067 取石子游戏(博弈论)

    Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石子.游戏规定,每次有两种不同的取法,一是可以在任意的一堆中取走任意多的石子:二是可以在两堆中同时取走相同数量的石子.最后 ...

  9. 【kmp+最小循环节】poj 2406 Power Strings

    http://poj.org/problem?id=2406 [题意] 给定字符串s,s=a^n,a是s的子串,求n最大是多少 [思路] kmp中的next数组求最小循环节的应用 例如 ababab ...

随机推荐

  1. gcc编译C++程序

    gcc动态编译和静态编译方法 一.单个源.cpp文件生成可执行程序下面是一个保存在文件 helloworld.cpp 中一个简单的 C++ 程序的代码: /* helloworld.cpp */ #i ...

  2. Android程序启动程序与页面的跳转

    package login; import com.example.login.R; import android.app.Activity; import android.content.Inten ...

  3. HDOJ 1590

    #include<stdio.h> #include<iostream> #include<stdlib.h> #include<string.h> u ...

  4. Java字符串split函数的注意事项

    Java字符串的split方法可以分割字符串,但和其他语言不太一样,split方法的参数不是单个字符,而是正则表达式,如果输入了竖线(|)这样的字符作为分割字符串,会出现意想不到的结果, 如, Str ...

  5. Rehashing

    The size of the hash table is not determinate at the very beginning. If the total size of keys is to ...

  6. Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  7. Backpack | & ||

    Backpack | Given n items with size Ai, an integer m denotes the size of a backpack. How full you can ...

  8. 【转】SQL中内连接和外连接

    如表     -------------------------------------------------     table1 | table2 |     ----------------- ...

  9. 让Delphi的DataSnap发挥最大效率

    让Delphi的DataSnap发挥最大效率 让Delphi的DataSnap发挥最大效率 一个DataSnap的应用程序由两个层组成: DataSnap服务器,它有一个带有一个或者更多DataSet ...

  10. poj_2674 弹性碰撞

    题目大意 给定一条直线,长度为L 表示区间[0, L].在直线上开始放置N个人,每个人有一个初始位置pos(用到直线上端点0的距离表示)和初始方向dir('p' 或 'P' 表示向端点L行走, 'n' ...