思路分析
挺裸了一题,在末尾节点存一个multiset,查询的时候跳last
AC代码
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define fun function
#define sz(x) (int)(x).size()
#define lowbit(x) (x)&(-x)
#define all(x) (x).begin(),(x).end()
#define mem(a,b) memset(a,b,sizeof(a))
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
namespace FastIO {
#define BUF_SIZE 100000
#define OUT_SIZE 100000
bool IOerror=0;
inline char nc() {
static char buf[BUF_SIZE],*p1=buf+BUF_SIZE,*pend=buf+BUF_SIZE;
if(p1==pend) {
p1=buf;
pend=buf+fread(buf,1,BUF_SIZE,stdin);
if(pend==p1) {
IOerror=1;
return -1;
}
}
return *p1++;
}
inline bool blank(char ch) {
return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';
}
template<class T> inline bool read(T &x) {
bool sign=0;
char ch=nc();
x=0;
for(; blank(ch); ch=nc());
if(IOerror)return false;
if(ch=='-')sign=1,ch=nc();
for(; ch>='0'&&ch<='9'; ch=nc())x=x*10+ch-'0';
if(sign)x=-x;
return true;
}
template<class t> inline void print(t x) {
if (x < 0) putchar('-'), print(-x);
else {
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
}
template<class T,class... U>bool read(T& h,U&... t) {
return read(h)&&read(t...);
}
#undef OUT_SIZE
#undef BUF_SIZE
};
using namespace std;
using namespace FastIO;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef long long ll;
typedef pair<int,int> pii;
typedef unsigned long long ull;
const int INF = 0x3f3f3f3f;
const int N = 1e6+10;
int ed[N],w[N];
struct AC_auto {
struct T {
int son[26],fail,word,last;
multiset<int>vals;
void clear() {
mem(son,0);
last = fail = 0;
word = -1;
}
} tree[N];
int cnt = 1;
int newnode() {
++cnt;
tree[cnt].clear();
return cnt;
}
void init() {
cnt = 1;
tree[1].clear();
tree[1].word = 0;
}
void ins(string s,int id) {
int p = 1;
for(auto i:s) {
int c = i-'a';
if(!tree[p].son[c]) tree[p].son[c] = newnode();
p = tree[p].son[c];
}
tree[p].vals.insert(0);
tree[p].word = 0;
ed[id] = p;
}
void modify(int x,int y) {
int p = ed[x];
tree[p].vals.erase(tree[p].vals.find(w[x]));
w[x] = y;
tree[p].vals.insert(y);
tree[p].word = *tree[p].vals.rbegin();
}
void getfail() {
for(int i=0; i<26; i++) tree[0].son[i] = 1;
queue<int>q;
q.push(1);
while(!q.empty()) {
int u = q.front();
q.pop();
int ufail = tree[u].fail;
tree[u].last = tree[ufail].word>=0?ufail:tree[ufail].last;
for(int i=0; i<26; i++) {
int v = tree[u].son[i];
if(v) {
tree[v].fail = tree[ufail].son[i];
q.push(v);
} else tree[u].son[i] = tree[ufail].son[i];
}
}
}
int query(string s) {
int ans = -1;
int p = 1;
for(auto i:s) {
p = tree[p].son[i-'a'];
for(int j = p; j!=1; j = tree[j].last) {
ans = max(ans,tree[j].word);
}
}
return ans;
}
} AC;
signed main() {
#ifdef xiaofan
freopen("1.in","r",stdin);
freopen("1.out","w",stdout);
#endif
IOS;
int n,q;
cin>>n>>q;
AC.init();
for(int i=1; i<=n; i++) {
string s;
cin>>s;
AC.ins(s,i);
}
AC.getfail();
while(q--) {
int op;
cin>>op;
if(op == 1) {
int x,y;
cin>>x>>y;
AC.modify(x,y);
} else {
string s;
cin>>s;
cout<<AC.query(s)<<"\n";
}
}
return 0;
}