POJ 2406-Power Strings (KMP)__SDUT

來源:互聯網
上載者:User
Power Strings Time Limit: 1000ms   Memory limit: 65536K  有疑問。點這裡^_^ 題目描述 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). 輸入   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.
輸出   For each s you should print the largest n such that s = a^n for some string a.
樣本輸入
abcdaaaaababab.
樣本輸出
143
提示   This problem has huge input, use scanf instead of cin to avoid time limit exceed.
來源   樣本程式


一道KMP的變形問題。大體意思是判斷字串最多由幾部分重複串組成。

多思考,一遍有一遍的收穫。



#include<stdio.h>#include<string.h>char str[1000010];int next[1000010],len;void getnext()// 算是KMP得到next[]的模版{    int i = 0,j = -1;    next[0] = -1;    while(i < len)    {        if(j == -1 || str[i] == str[j])           {               i++;               j++;               next[i]=j;           }        else            j = next[j];    }}int main(){    while(scanf("%s",str)!=EOF)    {        if(str[0] == '.')            break;        len = strlen(str);        getnext();        if((len % (len - next[len]))==0 )//關鍵,仔細領悟            printf("%d\n",len / (len - next[len]));        else            printf("1\n");    }    return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.