POJ 2752

简介: //KMP,对vector单个赋值不懂,只能用c语言形式拉 //大致题意:字符串s有多少个子串既是前缀又是后缀 #include #include #include #include using namespace std; const int N = 400010; ...
//KMP,对vector单个赋值不懂,只能用c语言形式拉 
//大致题意:字符串s有多少个子串既是前缀又是后缀 
#include <iostream>
#include <string>
#include <cstring>
#include <cstdlib>
using namespace std;
const int N = 400010;
int next[N] = {0},a[N] = {0};
void get_next(string s)
 {
     int i,j;
     i=0;
     j=-1;
     next[0]=-1;
     while(i<s.length())
     {
         if(j==-1||s[i]==s[j])
         {
             ++i;
             ++j;
             next[i]=j;
         }
         else
             j=next[j];
     }
 }
int main()
{
    int i,j,k,T;
    string s;
    while(cin>>s)
    {
        get_next(s);
        k = s.length();
        j =0;
        while(k>0)
        {
            a[j++] = next[k];
            k = next[k];
        }
        for(i=j-2;i>=0;i--)//a[j-1]等于0 
            cout<<a[i]<<" ";                       
        cout<<s.length()<<endl;
        memset(a,0,sizeof(a));
        memset(next,0,sizeof(next));
        s.clear();
    }
  //  system("pause");
    return 0;
}
        
        
        

 

目录
相关文章
|
算法框架/工具
POJ 2262 Goldbach's Conjecture
POJ 2262 Goldbach's Conjecture
136 0
POJ 2487 Stamps
POJ 2487 Stamps
102 0
POJ 1804
题目:http://poj.org/problem?id=1804 大意:给你一串数字,排序。求出最少的交换次数  \ 我用归并做的 #include #include using namespace std; int aa[500010],bb[500010]; long lon...
695 0
POJ 2027 No Brainer
Problem Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indicating the number of data sets.
866 0
|
人工智能
POJ 1936 All in All
Description You have devised a new encryption technique which encodes a message by inserting between its characters randomly generated strings in a clever way.
788 0