zoukankan      html  css  js  c++  java
  • 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description

    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:) 

    Input

    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. 

    Output

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

    Sample Input

    ababcababababcabab
    aaaaa
    

    Sample Output

    2 4 9 18
    1 2 3 4 5
    

    Source

    【分析】
    简单的应用,转个弯就过来了。
    从总长度开始一直next就可以了。注意总长度也是符合条件的
     1 /*
     2 登科后
     3 唐代
     4 孟郊
     5 
     6 昔日龌龊不足夸,今朝放荡思无涯。 
     7 春风得意马蹄疾,一日看尽长安花。 
     8 */
     9 #include <iostream>
    10 #include <cstdio>
    11 #include <algorithm>
    12 #include <cstring>
    13 #include <vector>
    14 #include <utility>
    15 #include <iomanip>
    16 #include <string>
    17 #include <cmath>
    18 #include <queue>
    19 #include <assert.h>
    20 #include <map>
    21 #include <ctime>
    22 #include <cstdlib>
    23 #include <stack>
    24 #define LOCAL
    25 const int MAXN = 400000 + 10;
    26 const int INF = 100000000;
    27 const int SIZE = 450;
    28 const int MAXM = 1000000 + 10;
    29 const int maxnode =  0x7fffffff + 10;
    30 using namespace std;
    31 int l1, l2;
    32 char a[MAXN];
    33 int next[MAXN];//不用开太大了.. 
    34 int Ans[MAXN];
    35 void getNext(){
    36      //初始化next数组 
    37      next[1] = 0;
    38      int j = 0;
    39      for (int i = 2; i <= l1; i++){
    40          while (j > 0 && a[j + 1] != a[i]) j = next[j];
    41          if (a[j + 1] == a[i]) j++;
    42          next[i] = j;
    43      }
    44      return;
    45 }
    46 /*int kmp(){
    47     int j = 0, cnt = 0;
    48     for (int i = 1; i <= l2; i++){
    49         while (j > 0 && a[j + 1] != b[i]) j = next[j];
    50         if (a[j + 1] == b[i]) j++;
    51         if (j == l1){
    52            cnt++;
    53            j = next[j];//回到上一个匹配点 
    54         }
    55     }
    56     return cnt;
    57 }*/
    58 
    59 /*void init(){
    60      scanf("%s", a + 1);
    61      scanf("%s", b + 1);
    62      l1 = strlen(a + 1);
    63      l2 = strlen(b + 1);
    64 }*/
    65 
    66 int main(){
    67     int T;
    68     
    69     while (scanf("%s", a + 1) != EOF){
    70           int tot = 0;
    71           l1 = strlen(a + 1);
    72           getNext();
    73           int tmp = next[l1];
    74           if (tmp != 0) Ans[tot++] = tmp;
    75           while (next[tmp] != 0){
    76                 tmp = next[tmp];
    77                 Ans[tot++] = tmp; 
    78           } 
    79           for (int i = tot - 1; i >= 0; i--) printf("%d ", Ans[i]); 
    80           printf("%d
    ", l1);
    81     }
    82     /*scanf("%s", a + 1);
    83     l1 = strlen(a + 1);
    84     getNext();
    85     for (int i = 1; i <= l1; i++) printf("%d" , next[i]);*/
    86     return 0;
    87 }
    View Code
  • 相关阅读:
    【转】PostgreSQL与MySQL比较
    HIVE出现Read past end of RLE integer from compressed stream Stream for column 1 kind LENGTH position: 359 length: 359 range: 0错误
    HSDF查看各级目录的大小
    windows7搜索python java go php等其他文件内容
    Tomcat配置https后,并发较大时,频繁超时情况。
    Tomcat7配置Https
    部分手机浏览器存在将ajax请求当成广告过滤的情况,及解决方案
    百度广告联盟api probuf协议对接
    SQL查询时,根据日期范围查询周
    执行Hive出现Error running child : java.lang.OutOfMemoryError: Java heap space错误
  • 原文地址:https://www.cnblogs.com/hoskey/p/4333433.html
Copyright © 2011-2022 走看看