Time Limit: 3000MS | Memory Limit: 30000K | |
Total Submissions: 16988 | Accepted: 8172 |
Description
Input
number zero on it.
Output
Sample Input
3 aaa 12 aabaabaabaab 0
Sample Output
Test case #1 2 2 3 3 Test case #2 2 2 6 2 9 3 12 4
這幾天一直在做最大流問題的題目,發現有點做不動,所以就一直沒有更新博客。
也是這幾個星期要考試的緣故沒有太多的時間去學更多的東西, 以後一定補上。
這個題就是一個KMP的題目
題意就是讓你找字符串的週期
如例2
前兩個字母有兩個週期,單週期爲a
前六個字母也有兩個週期,單週期爲aab
前九個字母有三個週期,單週期爲aab
....
代碼:
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <iostream>
using namespace std;
char ch[1000005];
int next[1000005];
void Get(char *s, int next[])
{
int i, j;
i = 0, j = -1;
next[0] = -1;
while ( s[i] )
{
if ( j == -1||s[i] == s[j] )
{
i++, j++;
next[i] = j;
}
else
j = next[j];
}
}
int main()
{
int n, i;
int Num = 1;
while ( ~scanf ( "%d", &n )&& n != 0 )
{
scanf ( "%s", ch );
Get( ch, next );
printf ( "Test case #%d\n", Num++ );
for ( i = 1;i <= n; i++ )
{
if ( i%(i-next[i]) == 0&&i != i-next[i] )
printf ( "%d %d\n", i, i/(i-next[i]) );
}
printf ( "\n" );
}
return 0;
}
代碼菜鳥,如有錯誤,請多包涵!!!
如有幫助記得支持我一下,謝謝!!!