2013
11-11

# Seek the Name, Seek the Fame

The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

Step1. Connect the father’s name and the mother’s name, to a new string S.

Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

Example: Father=’ala’, Mother=’la’, we have S = ‘ala’+'la’ = ‘alala’. Potential prefix-suffix strings of S are {‘a’, ‘ala’, ‘alala’}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)

The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby’s name.

ababcababababcabab
aaaaa


2 4 9 18
1 2 3 4 5


//* @author:
import java.util.*;
public class Main{
static final int  MAX=400001;
static int next[]=new int[MAX];
static int a[]=new int[MAX];

public static void main(String args[]){
int i, j, len, sum;
Scanner sc=new Scanner(System.in);

while(sc.hasNext())
{
String s=sc.next();
len=s.length();
getNext(s);
sum = 0;
j = len;
while(j != 0)
{
a[++sum] = j;
j = next[j];
}
for (i = sum; i > 0; i--) System.out.printf("%d ",a[i]);
System.out.printf("\n");
}

}
public static void getNext(String T) {//建立模式串T的next[]表
int i = 0;
int j = next[0] = -1;

while (i< T.length())
if (0 > j || T.charAt(i) == T.charAt(j)) {//匹配
j++; i++;
next[i] = j;
}else//失配
j = next[j];
}

}

1. bottes vernies blanches

I appreciate the efforts you men and women place in to share blogs on such sort of matters, it was certainly useful. Keep Posting!

2. 5.1处，反了；“上一个操作符的优先级比操作符ch的优先级大，或栈是空的就入栈。”如代码所述，应为“上一个操作符的优先级比操作符ch的优先级小，或栈是空的就入栈。”