題目鏈接:點擊查看
題目大意:給出后綴自動機的定義以及各部分的參數,要求模擬后綴自動機的各個部分
題目分析:直接按照后綴自動機的定義模擬就好了,因為字符串的長度只有50,所以可以用n^3的算法暴力,還可以用二進制記錄endpos,配合stl寫起來就非常簡單了
代碼:
?
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<sstream>
using namespace std;typedef long long LL;typedef unsigned long long ull;const int inf=0x3f3f3f3f;const int N=110;string s;map<LL,string>longest,shortest;map<string,LL>node;void init()
{for(int i=0;i<s.size();i++)//枚舉起點for(int j=1;j+i<=s.size();j++)//枚舉長度{string sub=s.substr(i,j);//確定子串LL st=0;for(int k=0;k+j<=s.size();k++)//枚舉所屬endpos的起點 {if(s.substr(k,j)==sub)st|=1LL<<(k+j);} node[sub]=st;if(shortest[st].empty()||j<shortest[st].size())shortest[st]=sub;if(j>longest[st].size())longest[st]=sub;}
}int main()
{
//#ifndef ONLINE_JUDGE
// freopen("input.txt","r",stdin);
//#endifios::sync_with_stdio(false);cin>>s;init();int w;cin>>w;while(w--){string s;cin>>s;LL st=node[s];cout<<shortest[st]<<' '<<longest[st];int pos=0;while(st){if(st&1)cout<<' '<<pos;pos++;st>>=1;}cout<<endl;}return 0;
}
?
總結
以上是生活随笔為你收集整理的HihoCoder - 1441 后缀自动机一·基本概念(模拟,后缀自动机入门好题)的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。