poj------2406 Power Strings

來源:互聯網
上載者:User

標籤:des   style   blog   http   java   color   strong   io   

A - Power Strings 難度:☆☆Time Limit:3000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64uSubmit Status Practice POJ 2406

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.

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

考察kmp演算法中的next數組....

next的兩種表示方法,第一種是首碼next[]數組...

while(i<len){    if(-1==j||ss[i]==ss[j])    {        i++;        j++;        next[i]=j;     }    else  j=next[j];}

 

另種表示方法文為:

while(i<len){   if(j==-1||ss[i]==s[j]) {     i++:     j++;     if(ss[i]==ss[j])    {         next[i]=next[j];    }    else  next[i]=j; }  else  j=next[j];}

 

 這道題是考察有多少個重複的最大n所以我們不妨看他的回溯長度len_D=len(已經匹配的位置) --next[len](對應部分的匹配值);

 

Java代碼:

  

 1 //package dek0; 2  3 import java.util.Scanner; 4  5 public class Main {     6      7     public static void main(String args[]) 8     { 9         Scanner  reader = new Scanner(System.in);10         String ss="";11         while(reader.hasNext())    12         {13            ss=reader.next();14            if(ss.charAt(0)==‘.‘) break;15            int len=ss.length();16            int next[]=new int [len+1];17                next[0]=-1;18            int i=0,j=-1;19            while(i<len)20            {21                if(j==-1||ss.charAt(i)==ss.charAt(j))22                {23                  i++;24                  j++;25             /*     if(ss.charAt(i)==ss.charAt(j))26                          next[i]=next[j];27                  else      next[i]=j;*/28                  next[i]=j;29                }30                else  j=next[j];31            }32            if(len%(len-next[len])==0)33              System.out.println(len/(len-next[len]));34            else35                System.out.println(1);36         }37     }38 }

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.