HDU
来源:互联网 发布:js获取手机app版本号 编辑:程序博客网 时间:2024/06/06 18:53
HDU - 4622 Reincarnation
给定字符串S,有q次查询,每次查询[l, r]内的子串的数量。
#include <stdio.h>#include <string.h>#include <iostream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <set>#include <map>#include <string>#include <math.h>#include <stdlib.h>#include <time.h>#include <bitset>#define INF 0x3f3f3f3f#define eps 1e-6#define PI 3.1415926#define mod 1000000009#define base 2333using namespace std;typedef long long LL;const int maxn = 2e3 + 10;const int maxx = 1e3 + 10;inline void splay(int &v) { v=0;char c=0;int p=1; while(c<'0' || c >'9'){if(c=='-')p=-1;c=getchar();} while(c>='0' && c<='9'){v=(v<<3)+(v<<1)+c-'0';c=getchar();} v*=p;}struct Node { Node *pre, *nxt[26]; int step; void Clear() { step = 0; pre = NULL; memset(nxt, NULL, sizeof(nxt)); } int calc() { if(pre == NULL) return 0; return step - pre->step; }} *root, *last;Node st[maxn<<1], *cur;char str[maxn];int t, q, l, r, cnt, ans[maxn][maxn];void init() { cnt = 0; cur = st; root = last = cur++; root->Clear();}void extend(int w) { Node *np = cur++, *p = last; np->Clear(); np->step = p->step+1; while(p && !p->nxt[w]) p->nxt[w] = np, p = p->pre; if(p == NULL) { np->pre = root; cnt += np->calc(); } else { Node *q = p->nxt[w]; if(q->step == p->step+1) { np->pre = q; cnt += np->calc(); } else { Node *nq = cur++; nq->Clear(); memcpy(nq->nxt, q->nxt, sizeof(q->nxt)); cnt -= q->calc(); nq->step = p->step+1; nq->pre = q->pre; np->pre = q->pre = nq; cnt += q->calc()+np->calc()+nq->calc(); while(p && p->nxt[w] == q) p->nxt[w] = nq, p = p->pre; } } last = np;}void solve() { scanf("%d", &t); while(t--) { scanf("%s", str); int len = strlen(str); for(int i = 0; i < len; i++) { init(); for(int j = i; j < len; j++) { extend(str[j]-'a'); ans[i][j] = cnt; } } splay(q); while(q--) { splay(l), splay(r); printf("%d\n", ans[l-1][r-1]); } }}int main() { //srand(time(NULL)); //freopen("kingdom.in","r",stdin); //freopen("kingdom.out","w",stdout); solve();}
阅读全文
1 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- 关于Markdown,吞文本问题
- Android应用架构
- 设计模式初探-单例模式
- 三门问题(Monty Hall problem)背后的贝叶斯理论
- iOS测试之接口测试总结
- HDU
- Codeforces 432D Prefixes and Suffixes (next数组的应用)
- Android TV使用属性动画绘制平移焦点框
- 【Laravel Excel译文】——导出
- tcpdump实用用法
- 最常见的管理误区,你中招了吗?
- Gym 101194F Mr. Panda and Fantastic Beasts
- JavaScript(4)__Js基础<字符串>
- 澳门拥抱阿里云:全面应用智能技术,为城市装上“大脑”