poj 2752

来源:互联网 发布:淘宝自动回复短语大全 编辑:程序博客网 时间:2024/05/20 17:10
#include <iostream>  #include <cstdio>  #include <cstring>  using namespace std;    int Next[400005];  char str[400005];  int ans[400005];  int cnt;  int len;    void getNext()  {      Next[0] = -1;      int i = 0, j = -1;      while (i < len)      {          if (j == -1 || str[i] == str[j])          {              ++i;              ++j;              Next[i] = j;          }          else j = Next[j];      }  }    int main()  {      while (scanf("%s", str) != EOF)      {          len = strlen(str);          getNext();          cnt = 0;          int t = Next[len - 1];          while (t != -1)          {              if (str[t] == str[len - 1]) ans[cnt++] = t + 1;              t = Next[t];          }          for (int i = cnt - 1; i >= 0; --i)          {              printf("%d ", ans[i]);          }          printf("%d\n", len);      }      return 0;  }  

0 0
原创粉丝点击