zoukankan      html  css  js  c++  java
  • POJ 2752 Seek the Name, Seek the Fame

    题意:找整个串的前缀与后缀相等,从小到大输出

    解题思路:Next[]数组的运用,只要回溯Next[]数组就行了,你自己写个例子一下子就懂了(本人懒,就不帮你写了)

     1 #include <iostream>
     2 #include <cstring>
     3 #include <string>
     4 #include <algorithm>
     5 #include <cstdio>
     6 #define MAXSIZE 1000100
     7 using namespace std;
     8 
     9 char a[MAXSIZE];
    10 int Next[MAXSIZE];
    11 int Res[MAXSIZE];
    12 int Len, Len_Res;
    13 
    14 int GetNext()
    15 {
    16     int i = 0, j = Next[0] = -1;
    17     while (i < Len)
    18     {
    19         if (j == -1 || a[i] == a[j])
    20             Next[++i] = ++j;
    21         else
    22             j = Next[j];
    23     }
    24 }
    25 
    26 void GetRes()
    27 {
    28     Len_Res = 0;
    29     int p = Len;
    30     while (p)
    31     {
    32         Res[Len_Res++] = p;
    33         p = Next[p];
    34     }
    35 }
    36 
    37 int main(void)
    38 {
    39     ios::sync_with_stdio(false);
    40     while (cin >> a)
    41     {
    42         Len = strlen(a);
    43         GetNext();
    44         GetRes();
    45         for (int i = Len_Res - 1; i >= 0; --i)
    46             cout << Res[i] << ' ';
    47         cout << endl;
    48     }
    49 
    50     return 0;
    51 }
  • 相关阅读:
    扫描线与悬线
    随机搜索与模拟退火
    树的直径相关
    分数规划及斜率优化
    数学-剩余系
    后缀数据结构
    AC自动机和KMP
    生命游戏和随机数之间某种不可言说的秘密
    转移了
    BZOJ 1710: [Usaco2007 Open]Cheappal 廉价回文
  • 原文地址:https://www.cnblogs.com/ducklu/p/8962074.html
Copyright © 2011-2022 走看看