POJ2406KMP

来源:互联网 发布:倒计时软件哪个好 编辑:程序博客网 时间:2024/05/17 22:48
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.util.Scanner;import java.util.Stack;import java.util.StringTokenizer;public class Main {        public static void main(String[] args) {                InputReader in = new InputReader(System.in) ;                PrintWriter out = new PrintWriter(System.out);                String word ;                while(! (word = in.next()).equals(".")){                      out.println( new KMP(word.toCharArray()).Power() ) ;                    //  out.flush() ;                }                out.flush() ;        }}class KMP{      int m ;      char[] p ;      int[] fail ;      KMP(char[] p){          m = p.length ;          this.p = p ;          fail = new int[m+1] ;          int crt = fail[0] = -1 ;          for(int i = 1 ; i <= m ; i++){               while(crt >= 0 && p[crt] != p[i-1]) crt = fail[crt] ;               fail[i] = ++ crt ;          }      }      public int Power(){             if(m % (m - fail[m]) == 0) return  m / (m - fail[m]) ;             return 1 ;       }      public int searchForm(char[] txt){             int n = txt.length , count = 0 ;             for(int i = 0 , j = 0 ; i < n ; i++){                   while(j >= 0 && txt[i] != p[j]) j = fail[j] ;                   if(++j == m){                         count++ ;                         j = fail[j] ;                         //j = 0 ;                   }             }             return  count ;      }}class InputReader {    public BufferedReader reader;    public StringTokenizer tokenizer;    public InputReader(InputStream stream) {        reader = new BufferedReader(new InputStreamReader(stream), 32768);        tokenizer = null;    }    public String next() {        while (tokenizer == null || !tokenizer.hasMoreTokens()) {            try {                tokenizer = new StringTokenizer(reader.readLine());            } catch (IOException e) {                throw new RuntimeException(e);            }        }        return tokenizer.nextToken();    }    public int nextInt() {        return Integer.parseInt(next());    }}
0 0