HDU5790-Prefix

来源:互联网 发布:程序员眼里只有两种人 编辑:程序博客网 时间:2024/05/26 09:53

Prefix

                                                                      Time Limit: 2000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
                                                                                                Total Submission(s): 922    Accepted Submission(s): 277


Problem Description
Alice gets N strings. Now she has Q questions to ask you. For each question, she wanna know how many different prefix strings between Lth and Rth strings. It's so easy right? So solve it!
 

Input
The input contains multiple test cases.

For each test case, the first line contains one integer N(1N100000). Then next N lines contain N strings and the total length of N strings is between 1 and 100000. The next line contains one integer Q(1Q100000). We define a specail integer Z=0. For each query, you get two integer L, R(0=<L,R<N). Then the query interval [L,R] is [min((Z+L)%N,(Z+R)%N)+1,max((Z+L)%N,(Z+R)%N)+1]. And Z change to the answer of this query.
 

Output
For each question, output the answer.
 

Sample Input
3abcababaa30 20 11 1
 

Sample Output
763
 

Author
ZSTU
 

Source
2016 Multi-University Training Contest 5
 

Recommend
wange2014
 


题意:给你n个字符串,有q次询问,每次询问L~R字符串中不重复的前缀个数

解题思路:字典树+主席树,用一个pre数组记录每种前缀上次出现的位置,遍历所有字符串,每次这种前缀上次出现的位置减一,加上这种前缀出现的位置加一,对每次询问只要找出R位置树中,出现位置为L~R的前缀数量即可



#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>#include <functional>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;const int N = 1e5 + 10;int n, q, cnt;int f[N][26], pre[N];int s[N], L[N * 40], R[N * 40], sum[N * 40], tot;char ch[N];void update(int pre, int &now, int l, int r, int p, int val){sum[now = ++tot] = sum[pre] + val, L[now] = L[pre], R[now] = R[pre];if (l == r) return;int mid = (l + r) >> 1;if (p <= mid) update(L[pre], L[now], l, mid, p, val);else update(R[pre], R[now], mid + 1, r, p, val);}int query(int k, int l, int r, int ll, int rr){if (l >= ll&&r <= rr) return sum[k];int ans = 0, mid = (l + r) >> 1;if (ll <= mid) ans += query(L[k], l, mid, ll, rr);if (rr > mid) ans += query(R[k], mid + 1, r, ll, rr);return ans;}int main(){while (~scanf("%d", &n)){L[0] = R[0] = sum[0] = s[0] = cnt = tot = 0;memset(f, 0, sizeof f);for (int i = 1; i <= n; i++){scanf("%s", ch);for (int j = 0, k = 0; ch[j]; j++){if (!f[k][ch[j] - 'a']){f[k][ch[j] - 'a'] = ++cnt;memset(f[cnt], 0, sizeof f[cnt]);pre[cnt] = 0;}k = f[k][ch[j] - 'a'];if (pre[k]){if(!j) update(s[i - 1], s[i], 1, n, pre[k], -1);else update(s[i], s[i], 1, n, pre[k], -1);update(s[i], s[i], 1, n, i, 1);}else{if(!j) update(s[i - 1], s[i], 1, n, i, 1);else update(s[i], s[i], 1, n, i, 1);}pre[k] = i;}}int ans = 0, l, r;scanf("%d", &q);while (q--){scanf("%d%d", &l, &r);int ll = (ans + l) % n + 1, rr = (ans + r) % n + 1;if (ll > rr) swap(ll, rr);printf("%d\n", ans = query(s[rr], 1, n, ll, rr));}}return 0;}

原创粉丝点击