版本比较
海亮 2026暑 VII
正文
增加 180 行删除 0 行
907 行未改动全部展开90890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091907 行未改动全部展开 cout<<work()<<endl; }}```## [USACO08DEC] Secret Message G要考虑这俩种情况:
- 已有消息是查询消息的前缀;- 查询消息是已有消息的前缀。
考虑建字典树,对于树上的每个位置,要统计:- 有多少消息在这里结束- 到这里是多少消息的前缀
于是我们统计答案时,用 已有消息是查询消息前缀 + 查询消息是已有消息前缀 - 重复的(也就是自己)```cpp#include <bits/stdc++.h>using namespace std;#define ll long long#define db double#define pii pair<int,int>#define mp make_pair#define fi first#define se secondconst int N=5e5+10;int tr[N][2];int ed[N];int book[N];signed main(){ int sti=clock();
// freopen(".in","r",stdin);// freopen(".out","w",stdout); ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int m,n; cin>>m>>n; memset(tr,-1,sizeof tr); int cnt=0; for(int i=1;i<=m;i++) { int l;int now=0; cin>>l; for(int j=1;j<=l;j++) { int x; cin>>x; if(tr[now][x]==-1) tr[now][x]=++cnt; now=tr[now][x]; if(j==l) ed[now]++; book[now]++; } } for(int i=1;i<=n;i++) { int l; bool flag=0;int now=0;int pcnt=0; cin>>l; for(int j=1;j<=l;j++) { int x; cin>>x; if(flag) continue; if(tr[now][x]==-1) { cout<<pcnt<<endl; flag=1; continue; } now=tr[now][x]; pcnt+=ed[now]; if(j==l) { cout<<pcnt-ed[now]+book[now]<<endl; continue; } }
}
// cerr<<1.0*(clock()-sti)/CLOCKS_PER_SEC<<endl; return 0;}
```
## Radio Transmission 无线传输对于一个长度为 m 的字符串,如果它能由某个长度为 len 的字符串重复得到,那么它的最长公共前后缀长度一定是 m-len;而 KMP 求出的 kmp[m] 正是这个最长公共前后缀长度。
答案是 m-kmp[m]。
```cpp#include <bits/stdc++.h>using namespace std;
string s;string t;int nxt[1000010];
int main(){ int m; cin>>m; cin>>t; t=' '+t; int p; nxt[0]=0; p=0; for(int i=2;i<=m;i++) { while(t[p+1]!=t[i] && p) p=nxt[p]; if(t[p+1]==t[i]) p++; nxt[i]=p; } cout<<m-nxt[m];
return 0;}```
## [USACO15FEB] Censoring S用栈模拟删除,用 KMP 快速判断当前字符串末尾是否出现了 T。
我们从左到右加入字符,每加入一个字符,就让 KMP 维护“当前栈中字符串后缀和 T 前缀匹配了多少个字符”。
- 如果匹配长度达到 m,说明刚加入的字符让一个完整的 T 出现在末尾,于是直接弹出这 m 个字符,相当于删除它。
- 因为删除可能影响前面的匹配状态,所以用 f[i] 保存每个位置对应的 KMP 匹配长度,删除后恢复栈顶字符之前的状态。
```cpp#include <bits/stdc++.h>using namespace std;const int N=2e6+10;string s;string t;int nxt[N];int f[N];int main(){ stack<int> st; cin>>s; cin>>t; int n=s.size(); int m=t.size(); s=' '+s; t=' '+t; int p; nxt[0]=0; p=0; for(int i=2;i<=m;i++) { while(t[p+1]!=t[i] && p) p=nxt[p]; if(t[p+1]==t[i]) p++; nxt[i]=p; } p=0; for(int i=1;i<=n;i++) { while(t[p+1]!=s[i] && p) p=nxt[p]; if(t[p+1]==s[i]) p++; f[i]=p; st.push(i); if(p==m) { for(int k=1;k<=m;k++) if(!st.empty()) st.pop(); if(!st.empty()) p=f[st.top()]; } } string ans=""; while(!st.empty()) { ans+=s[st.top()]; st.pop(); } reverse(ans.begin(),ans.end()); cout<<ans<<endl; return 0;}```