bzoj2754: [SCOI2012]喵星球上的点名
生活随笔
收集整理的這篇文章主要介紹了
bzoj2754: [SCOI2012]喵星球上的点名
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
事實證明,我肉眼debug了兩個晚上,還是不及對拍效率高。
AC自動機寫錯了都毫不自覺的智障宸
因為數據水,寫的暴力
?
//Achen #include<algorithm> #include<iostream> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<queue> #include<cmath> #include<map> #define For(i,a,b) for(int i=(a);i<=(b);i++) #define Rep(i,a,b) for(int i=(a);i>=(b);i--) const int N=400007; typedef long long LL; using namespace std; int n,m,a[N],ll[N],rr[N],ans2[N],cnt[N];template<typename T>void read(T &x) {char ch=getchar(); x=0; T f=1;while(ch!='-'&&(ch<'0'||ch>'9')) ch=getchar();if(ch=='-') f=-1,ch=getchar();for(;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0'; x*=f; }map<int,int>ch[N]; #define IT map<int,int>::iterator int tot,rt,w[N],fail[N],ans[N]; void insert(int ql,int qr) {if(!rt) rt=++tot;int x=rt;For(i,ql,qr) {int c=a[i];if(!ch[x][c]) ch[x][c]=++tot;x=ch[x][c];} w[x]++; }queue<int>que; void get_fail() {que.push(rt);while(!que.empty()) {int x=que.front();que.pop();for(IT it=ch[x].begin();it!=ch[x].end();it++) {int y=it->second;if(x==rt) fail[y]=rt;else {int z=fail[x];for(;fail[z]&&!ch[z][it->first];z=fail[z]);if(ch[z][it->first]) fail[y]=ch[z][it->first];else fail[y]=rt;}que.push(y); }} }#define pr pair<int,int> pr sta[N]; int top; int qry(int ql,int qr) {int x=rt,rs=0;For(i,ql,qr) {int c=a[i];for(;fail[x]&&!ch[x][c];x=fail[x]);if(ch[x][c]) {x=ch[x][c]; if(w[x]) {rs+=w[x]; cnt[x]++;sta[++top]=make_pair(x,w[x]);w[x]=0; }int y=fail[x];while(w[y]) {rs+=w[y];sta[++top]=make_pair(y,w[y]);w[y]=0; cnt[y]++;y=fail[y];}}}while(top) {pr tp=sta[top--];w[tp.first]=tp.second;}return rs; } void calc(int ql,int qr) {int x=rt;For(i,ql,qr) {int c=a[i];x=ch[x][c];}printf("%d\n",cnt[x]); }//#define DEBUG int main() { #ifdef DEBUGfreopen("2754.in","r",stdin);freopen("2754.out","w",stdout); #endifread(n); read(m);For(i,1,n) {int len; read(len);ll[i]=a[0]+1;For(j,1,len) read(a[++a[0]]);a[++a[0]]=-1;read(len);For(j,1,len) read(a[++a[0]]);rr[i]=a[0];}For(i,1,m) {int len; read(len);ll[n+i]=a[0]+1;For(j,1,len) read(a[++a[0]]);rr[n+i]=a[0];insert(ll[n+i],rr[n+i]);}get_fail();For(i,1,n) ans[i]=qry(ll[i],rr[i]);For(i,1,m)calc(ll[n+i],rr[n+i]);For(i,1,n-1) printf("%d ",ans[i]);printf("%d",ans[n]);return 0; } /* 4 1 1 2 1 2 4 3 1 1 9 6 6 9 1 10 7 9 4 8 1 9 9 6 9 9 7 9 3 10 6 8 9 2 1 1 4 1 4 1 9 5 8 9 1 5 2 4 2 10 7 6 2 5 1 */ View Code?
轉載于:https://www.cnblogs.com/Achenchen/p/8561793.html
總結
以上是生活随笔為你收集整理的bzoj2754: [SCOI2012]喵星球上的点名的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Linux启动/停止/重启Mysql数据
- 下一篇: Java 403 forbidden错误