HDU5421-Victor and String

本文介绍了一道关于字符串操作的问题,包含添加字符到字符串两端及查询回文子串数量的操作。通过构建回文树的数据结构,高效地解决了该问题。

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

Victor and String

                                                                     Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/262144 K (Java/Others)
                                                                                               Total Submission(s): 585    Accepted Submission(s): 164


Problem Description
Victor loves to play with string. He thinks a string is charming as the string is a palindromic string.

Victor wants to play  n  times. Each time he will do one of following four operations.

Operation 1 : add a char  c  to the beginning of the string.

Operation 2 : add a char  c  to the end of the string.

Operation 3 : ask the number of different charming substrings.

Operation 4 : ask the number of charming substrings, the same substrings which starts in different location has to be counted.

At the beginning, Victor has an empty string.
 

Input
The input contains several test cases, at most  5  cases.

In each case, the first line has one integer  n  means the number of operations.

The first number of next  n  line is the integer  op , meaning the type of operation. If  op = 1  or  2 , there will be a lowercase English letters followed.

1n100000 .
 

Output
For each query operation(operation 3 or 4), print the correct answer.
 

Sample Input
  
6 1 a 1 b 2 a 2 c 3 4 8 1 a 2 a 2 a 1 a 3 1 b 3 4
 

Sample Output
  
4 5 4 5 11
 

Source
 

Recommend
hujie
 


题目:有n种操作,开始有一个空串,有4种操作:

1 c 在字符串的首部添加字符c

2 c 在字符串的尾部添加字符c

3 询问字符中回文串种类的个数

4 询问字符串中回文串的个数

解题思路:last[0]表示首部的操作的位置,last[1]表示尾部的操作的位置,当一整个字符串是回文串时,last[0]=last[1]



#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#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 maxn = 1e6 + 10;

char s[maxn];
int n, x;

struct PalindromicTree
{
	const static int maxn = 1e6 + 10;
	int next[maxn][26], last[2], sz, tot[2];
	int fail[maxn], len[maxn];
	LL cnt[maxn];
	char s[maxn];
	void Clear()
	{
		len[1] = -1; len[2] = 0;
		fail[2] = fail[1] = 1;
		last[0] = last[1] = (sz = 3) - 1;
		cnt[1] = cnt[2] = 0;
		tot[0] = (tot[1] = n - 1) + 1;
		memset(next[1], 0, sizeof(next[1]));
		memset(next[2], 0, sizeof(next[2]));
		memset(s, 0, sizeof s);
	}
	int Node(int length)
	{
		memset(next[sz], 0, sizeof(next[sz]));
		len[sz] = length, cnt[sz] = 1;
		return sz++;
	}
	int getfail(int x, int k)
	{
		while (s[tot[k]] != s[tot[k] + (k ? -1 : 1) * (len[x] + 1)]) x = fail[x];
		return x;
	}
	LL add(char pos, int k)
	{
		int x = (s[tot[k] += k ? 1 : -1] = pos) - 'a', y = getfail(last[k], k);
		if (next[y][x]) return cnt[last[k] = next[y][x]];
		last[k] = next[y][x] = Node(len[y] + 2);
		fail[last[k]] = len[last[k]] == 1 ? 2 : next[getfail(fail[y], k)][x];
		cnt[last[k]] += cnt[fail[last[k]]];
		if (len[last[k]] == tot[1] - tot[0] + 1) last[k ^ 1] = last[k];
		return cnt[last[k]];
	}
}solve;

int main()
{
	while (~scanf("%d", &n))
	{
		LL ans = 0;
		solve.Clear();
		for (int i = 1; i <= n; i++)
		{
			scanf("%d", &x);
			if (x <= 2)
			{
				scanf("%s", s);
				ans += solve.add(s[0], x - 1);
			}
			else printf("%lld\n", x == 3 ? 1LL * solve.sz - 3 : ans);
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值