HDU5790-Prefix

本文介绍了一道算法题目,使用字典树和主席树解决字符串查询问题。具体涉及如何构建字典树来存储字符串,并利用主席树快速查询特定区间内不同前缀的数量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
  
3 abc aba baa 3 0 2 0 1 1 1
 

Sample Output
  
7 6 3
 

Author
ZSTU
 

Source
 

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 long
const 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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值