HDU1358 Period【KMP】
Period
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13519 Accepted Submission(s): 6330
Problem Description
For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
Input
The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on it.
Output
For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.
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
問題描述:給定字串S(長度為N),問S的長度為 i (1<i<=n )的字首是否為迴圈串,如果是列印字首長度 i ,以及有多少個迴圈節。比如樣例一aaa。長度為2的字首為aa,迴圈節長度為1(即a),有2個迴圈節,則輸出2 2。長度為3的字首為aaa,迴圈節長度為1(即a),有3個迴圈節,則輸出3 3。
解題思路:對KMP中next陣列的運用。要記住迴圈節的長度為 i-next[i]。如果i是迴圈節長度的倍數(不能為1倍),則可輸出,具體看程式。
AC的C++程式:
#include<iostream>
#include<cstring>
using namespace std;
const int N=1000010;
char p[N];
int Next[N];
void GetNext(char p[],int next[])
{
next[0]=-1;
int k=-1,j=0,plen=strlen(p);
while(j<plen)
{
//p[k]表示字首 p[j]表示字尾
if(k==-1||p[k]==p[j])
{
k++;
j++;
next[j]=k;
}
else
k=next[k];
}
}
int main()
{
int n,cnt=1;
while(~scanf("%d",&n)&&n)
{
scanf("%s",p);
GetNext(p,Next);
printf("Test case #%d\n",cnt++);
for(int i=2;i<=n;i++)//列舉字首長度
{
int l=i-Next[i];//迴圈節的長度
if(i%l==0&&i/l>1)
printf("%d %d\n",i,i/l);
}
printf("\n");
}
return 0;
}
相關推薦
HDU1358 Period【KMP】
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13519 Accepted Submiss
poj1961 Period【KMP】
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 20436
HDU 1711 Number Sequence【kmp】
show turn case put next() void 出現 回首 img Given two sequences of numbers : a[1], a[2], ...... , a[N], and b[1], b[2], ...... , b[M] (1
關於字符串問題【KMP】
基礎 是你 一道 不存在 最短 bsp 問題 多人 相等 今天重點學了學字符串,算是為以前的字符串恐懼癥填坑(想想一個KMP都背不熟的lz) KMP很簡單,主要是維護p數組(定義:在一個字符串裏,1~p[i]這個串和i-p[i]+1~i這個串相等)對於字符串的匹配問題(求
hdu 3336 Count the string【kmp】
color efi 每一個 next oid std 字符串 hdu 自身 http://acm.hdu.edu.cn/showproblem.php?pid=3336 題意:給你一個字符串,問字符串每一個前綴在字符串中的出現總次數。 思路:kmp的應用,自身和自身進行
【KMP】字符串匹配
esp 一次 AD 難點 main 問題: clu space names 問題: 字符串s="ABBCABCDABDADSBC",p="ABCDABD",問p在s中第一次出現的索引,未找到則返回-1 思路: 暴力求解:時間復雜度O(m*n),其中m、n分別為s、p的長
【KMP】【字符串】KMP字符串匹配算法 學習筆記
出現 調用 隨機 rri 形象 再看 aaaaa scan i+1 一、簡介 KMP是由Knuth、Morris和Prat發明的字符串匹配算法,它的時間復雜度是均攤\(O(n+m)\)。其實用Hash也可以做到線性,只不過Hash存在極其微小的難以避免的沖突。於是就
【KMP】洛谷P2375 [NOI2014]動物園 題解
clu 控制 () borde 會有 strlen 觀察 約束 brush 一開始的方向應該對了,但是沒有想到合理的優化還是沒寫出來…… 題目描述 近日,園長發現動物園中好吃懶做的動物越來越多了。例如企鵝,只會賣萌向遊客要吃的。為了整治動物園的不良風氣,讓
hust 1010 The Minimum Length(循環節)【KMP】
col color mini sin length 就是 problem 一個 ext <題目鏈接> 題目大意: 有一個字符串A,一次次的重寫A,會得到一個新的字符串AAAAAAAA.....,現在將這個字符串從中切去一部分得到一個字符串B,例如有一個字符串A=
CH1809匹配統計【KMP】
位置 ostream 長度 void ons 同學 code abcd names 1809 匹配統計 0x18「基本數據結構」練習 描述 阿軒在紙上寫了兩個字符串,分別記為A和B。利用在數據結構與算法課上學到的知識,他很容易地求出了“字符串A從任意位置開始的後綴子串”與“
【KMP】演算法,未改進C++
首先是部分入門解釋: 1:求next陣列 當我們假設 模式串patten 為 aaabc時, a a a b c 對應的 NEXT陣列為: -1 0 1 2 0。 Next 陣列的含義: 求nex
【JZOJ5178】So many prefix?【KMP】【DP】
題目大意: 題目連結:https://jzoj.net/senior/#main/show/5178 題目圖片: http://wx3.sinaimg.cn/mw690/0060lm7Tly1fwlvu2xyz7j30mx05oq3e.jpg http://wx3.sinaimg.c
poj 2406 Power Strings【kmp】
kmp,根據next陣列的性質如果有答案的話就是n/(n-(ne[n]+1)),否則是1 搬來打算用SA後來發現必須用DC3就沒寫 #include<iostream> #include<cstdio> #include<cstring> using namespace
poj3080 Blue Jeans【KMP】【暴力】
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions:21746
poj3080——Blue Jeans【KMP】
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 21643
【KMP】【字串】BZOJ4974字串大師
分析: 在KMP演算法中,failfailfail指標有一個特殊的性質,i−failii-fail_ii−faili是前i個字元的最小迴圈節大小。 所以這題相當於就是說,給了你每個點的fail指標,求
【字串】【KMP】BZOJ3670動物園
分析: 不算太難的fail指標應用題。每次利用fail指標在上一個位置的匹配資訊,不停地向下匹配,如果超過一半則退回去即可。 #include<cstdio> #include<cst
【字串】【高斯消元】【KMP】BZOJ4820硬幣遊戲
分析: 如果資料範圍再小點,可以利用BZOJ1444有趣的遊戲方法來做。 所以這裡為了優化,直接儲存下來從某個字串轉移到另一個的概率即可。 #include<cstdio> #include
【矩陣加速】【數論】【KMP】[BZOJ1009][HNOI2008]GT考試
題目描述 Description 阿申準備報名參加GT考試,准考證號為N位數X1X2….Xn(0<=Xi<=9),他不希望准考證號上出現不吉利的數字。他的不吉利數學A1A2…Am(0<=Ai<=9)有M位,不出現是指X1X2…Xn
PKU 2406:Power Strings 【KMP】
Power Strings Time Limit : 6000/3000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 70 Accepted Subm