1 条题解

  • 0
    @ 2023-5-14 17:21:10

    KMP,KMP找出所有前缀等于后缀的,然后从NEXT[最后一位]开始跳,一直跳到没有,注意文件输入输出和输出排序,然后还有整个字符串长度算一个。 附上乱写的

    #include <iostream>
    #include <cstring>
    #include <vector>
    #include <algorithm>
    #include <cstdio>
    using namespace std;
    string a;
    int sot[400005];
    vector<int> pre(const string& str)
    {
    	int i=-1,j=0;
    	vector<int> ret;
    	ret.push_back(-1);
    	for (;j<(int)str.length();j++)
    	{
    		while (i!=-1&&str[i+1]!=str[j+1])
    		{
    			i=ret[i];
    		}
    		if (str[i+1]==str[j+1])
    		{
    			i++;
    		}
    		ret.push_back(i);
    	}
    	return ret;
    }
    int main ()
    {
    	freopen("a.in","r",stdin);
    	freopen("a.out","w",stdout);
    	while (cin>>a)
    	{
    	vector<int>ans=pre(a);
    	int cnt=0;
    	for (int i=ans[a.length()-1];i!=-1;i=ans[i]){
    		sot[cnt]=i;
    		cnt++;
    	}
    	for (int i=cnt-1;i>=0;i--)
    	cout<<sot[i]+1<<" ";
    	cout<<a.length()<<" ";
    	cout<<endl;
    }
    	return 0;
    }
    

    [字符串算法]Seek the Name, Seek the Fame

    信息

    ID
    299
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    (无)
    递交数
    10
    已通过
    1
    上传者