1. hihocoder #1441 : 后缀自动机一·基本概念
按照后缀自动机概念模拟即可, 复杂度$O(n^3logn)$.
#include <iostream> #include <map> #include <string> #include <set> using namespace std; const int N = 111; int n, cnt; string s; map<string,set<int> > suf_to_endpos; map<set<int>,int> endpos_id; map<string,int> suf_id; set<string> st[N]; string longest[N], shortest[N]; set<int> endpos[N]; int getID(set<int> &s) { if (endpos_id.count(s)) return endpos_id[s]; endpos[++cnt] = s; return endpos_id[s] = cnt; } int main() { cin>>s>>n; for (int i=0; i<s.size(); ++i) { for (int j=1; i+j<s.size(); ++j) { suf_to_endpos[s.substr(i,j)].insert(i+j); } } for (auto &t:suf_to_endpos) { int id = getID(t.second); string suf = t.first; st[id].insert(suf); suf_id[suf] = id; if (longest[id].empty()) longest[id] = shortest[id] = suf; else { if (suf.size()>longest[id].size()) longest[id] = suf; if (suf.size()<shortest[id].size()) shortest[id] = suf; } } while (n--) { cin>>s; int id = suf_id[s]; cout<<shortest[id]<<' '<<longest[id]; for (auto &&t:endpos[id]) cout<<' '<<t; cout<<endl; } }