a亚洲精品_精品国产91乱码一区二区三区_亚洲精品在线免费观看视频_欧美日韩亚洲国产综合_久久久久久久久久久成人_在线区

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

POj 2406 Power Strings

2019-11-14 10:23:12
字體:
供稿:網(wǎng)友

Description

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defined in the normal way: a^0 = "" (the empty string) and a^(n+1) = a*(a^n).

Input

Each test case is a line of input rePResenting s, a string of printable characters. The length of s will be at least 1 and will not exceed 1 million characters. A line containing a period follows the last test case.

Output

For each s you should print the largest n such that s = a^n for some string a.

Sample Input

abcdaaaaababab.

Sample Output

143

Hint

This problem has huge input, use scanf instead of cin to avoid time limit exceed.這道題是考察對next數(shù)組性質(zhì)的應(yīng)用。題意相當(dāng)于是求一個長字符串中循環(huán)節(jié)的個數(shù)。

  由于我們知道next數(shù)組中存的是一個位置(假設(shè)next[j]的值為k,對應(yīng)的字符串為M,如果k>0,那么M[0....k-1]和M[j-k.....j-1]是相同的,并且0...k-1這個序列一定是最長的),比如a b c a b c d(next值:-1 0 0 0 1 2 3 ),由next[6]=3可知,M[0..2]=M[3..6],這就找到了循環(huán)節(jié),于是我們思考從next數(shù)組作為切入點,來找到一種方法來求得循環(huán)節(jié)的個數(shù)。

  看看next數(shù)組的一個性質(zhì):next始終是從-1開始增加(在變?yōu)?之前)。這會導(dǎo)致一個有趣的現(xiàn)象:指針回溯的位置,最遠(yuǎn)都是在一個完整的循環(huán)節(jié)之后。其實由定義也能發(fā)現(xiàn),如果最遠(yuǎn)回溯到了字符串開頭,就會導(dǎo)致j=k,與next數(shù)組的定義中的0<k<j矛盾。這樣,就留出來了一個循環(huán)節(jié)的長度,如果總長度是這個循環(huán)節(jié)長度的整數(shù)倍,那么循環(huán)節(jié)的個數(shù)就是這個倍數(shù)。反之,說明這個字符串并不是在不停地循環(huán),而是在某些位置加入了一個或幾個不"和諧"的字符,導(dǎo)致指針無法回溯到第一個循環(huán)節(jié)之后,這樣,輸出1就可以了。

#include<stdio.h>#include<string.h>#define MAX_LEN 1000005int  get_next(void);char dest[MAX_LEN];int next[MAX_LEN];int main(){    while(scanf("%s",dest)!=EOF&&dest[0]!='.')    {    int len=get_next();    int flag=len%(len-next[len]);    if(flag==0)    {        printf("%d/n",len/(len-next[len]));    }    else    {        printf("1/n");    }    }    return 0;}int get_next(void){    int len=strlen(dest),i=0,j=-1;    next[0]=-1;    while(i<len)    {    if(j==-1||dest[i]==dest[j])    {        i++;j++;        next[i]=j;    }    else    {        j=next[j];    }    }    return len;}


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 欧美精品一区二区三区涩爱蜜 | 欧美日韩亚洲二区 | 日日精品 | 人人av在线 | 黄色小电影网址 | 国产精品乱码一区二区三区 | 国产福利精品一区二区三区 | 日韩欧美综合 | 91视频免费看网站 | 精品国产91乱码一区二区三区 | 亚洲日日操 | 久久久久亚洲精品 | 91丝袜| 一级片av | h在线看 | 日韩美女国产精品 | www.免费黄色 | 午夜精品一区二区三区在线视频 | 羞羞色视频 | 在线播放国产一区二区三区 | 欧美日韩国产一区二区三区 | 日本高清视频一区二区三区 | 在线视频一区二区三区 | 蜜臀av国产精品久久久久 | 日本久久网| 国产亚洲一区二区三区在线 | 亚洲欧美视频 | 欧美永久精品 | 久久精品亚洲精品 | 久久精品99国产精品日本 | 少妇裸体淫交免费视频 | 国产精品一区二区无线 | 国产精品久久久久久 | 91精品在线播放 | 亚洲精品乱码久久久久久国产主播 | 日韩中文字幕无码一区二区三区 | 亚洲视频在线观看一区二区三区 | 亚洲一级簧片 | 日本免费三片免费观看 | 欧美影 | 欧美视频中文字幕 |