tjut 4622
来源:互联网 发布:mac系统怎么卸载 编辑:程序博客网 时间:2024/05/21 17:21
#include <iostream> #include <string.h> #include <algorithm> #include <stdio.h> using namespace std; const int N=5010; struct State { State *pre,*go[26]; int step; void clear() { pre=0; step=0; memset(go,0,sizeof(go)); } int calc() { if(pre==0) return 0; return step-pre->step; } }*root,*last; State statePool[N*2],*cur; void init() { cur=statePool; root=last=cur++; root->clear(); } int tot; void Insert(int w) { State *p=last; State *np=cur++; np->clear(); np->step=p->step+1; while(p&&!p->go[w]) p->go[w]=np,p=p->pre; if(p==0) { np->pre=root; tot+=np->calc(); } else { State *q=p->go[w]; if(p->step+1==q->step) { np->pre=q; tot+=np->calc(); } else { State *nq=cur++; nq->clear(); memcpy(nq->go,q->go,sizeof(q->go)); tot-=p->calc()+q->calc(); nq->step=p->step+1; nq->pre=q->pre; q->pre=nq; np->pre=nq; tot+=p->calc()+q->calc()+np->calc()+nq->calc(); while(p&&p->go[w]==q) p->go[w]=nq, p=p->pre; } } last=np; } int ans[N][N]; char s[N]; void work() { scanf("%s",s); int n=strlen(s); for(int i=0;i<n;++i) { init(); tot=0; for(int j=i;j<n;++j) { Insert(s[j]-'a'); ans[i][j]=tot; } } int nQ; scanf("%d", &nQ); while (nQ--) { int l, r; scanf("%d%d", &l, &r); --l,--r; printf("%d\n", ans[l][r]); } } int main() { int T; cin>>T; while(T--) work(); return 0; }
0 0
- tjut 4622
- tjut 5289
- tjut 5288
- tjut 5294
- tjut 2586
- tjut 5296
- tjut 5297
- tjut 5299
- tjut 5384
- tjut 5387
- tjut 5386
- tjut 5381
- tjut 5400
- tjut 5399
- tjut 5396
- tjut 5398
- tjut 5412
- tjut 5410
- sdut oj1138 数据结构上机测试2-1:单链表操作A
- UIsearchBar的placeholder字体颜色和大小
- IPhone 自定义 UITableViewCell 行缩进
- 转载 感谢原作者 矩阵分解在推荐系统中的应用:NMF和经典SVD实战
- NOI 2016
- tjut 4622
- Buffer is too small
- 了解PCB
- HDU 2187 悼念512汶川大地震遇难同胞——老人是真饿了
- hdoj2141Can you find it?
- Cause: Already disposed: Module: 'app'
- 关于fragment和其他布局文件相结合问题
- markdown
- 稳固运行一年的logback配置