1344: PIPI的字符串问题Ⅱ

本文介绍了解决PIPI的字符串问题Ⅱ的方法,包括使用KMP算法判断一个字符串是否为另一个字符串的子串,并计算出现次数。此外,还介绍了如何通过字符串哈希在O(1)时间内求解相同问题。
1344: PIPI的字符串问题Ⅱ

题目描述

PIPI又来考察大家字符串处理的能力了。
给定一个字符串S,以及字符串T,你需要回答:
(1)T是否是S的子串。如果是输出YES,并回答问题(2);如果不是,输出NO。
(2)T作为S的子串在S中出现了几次?
请你来解决这个简单的问题。
注意:子串不是子序列。子串要求连续,如”abc"为"aabcc“的子串。

输入

第一行给出字符串S。|S|<=1e6.
第二行给出字符串T。|T|<=1e6.

输出

首先回答问题(1),若T为S子串,输出YES,第二行回答问题(2).若不是,直接输出NO。

样例输入

abcabcabc
bc

样例输出

YES
3

思路 1

KMP

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
char s[N], t[N];
int lens, lent, nxt[N];
//next
void buildNxt()
{
	int i = 0, j = -1;
	nxt[0] = -1;
	while(i < lent)
	{
		if(j == -1 || t[i] == t[j])
		{
			i++;
			j++;
			if(t[i] == t[j]) // i 如果失配了则,j 位置必然也失配,所以直接到滑动至 nxt[j]
				nxt[i] = nxt[j];
			else
				nxt[i] = j;
		}
		else
			j = nxt[j];
	}
}
int main()
{
	scanf("%s%s", s, t);
	lens = strlen(s);
	lent = strlen(t);
	buildNxt();
	int i = 0, j = 0, ans = 0;
	while(i < lens)
	{
		if(j == -1 || s[i] == t[j])
		{
			i++;
			j++;
			if(j == lent)
			{
				ans++;
				j = nxt[j];
			}
		}
		else
			j = nxt[j];
	}
	if(ans)
		printf("YES\n%d\n", ans);
	else
		printf("NO\n");
	return 0;
}

思路 2
字符串哈希: 将一个字符串转化成一个整数,并保证字符串不同,得到的哈希值也不同,可以在 o(1)的时间求出任意位置子串的哈希值。哈希方法:利用 unsigned long long 自然溢出,相当于自动对 2^64−1 取模。

如:s = “abcabcabc”,基数 base = 10,则,哈希数组 Hash[] = {0,1,12,123,1231,12312,123123,1231231,12312312,123123123}。[l,r] 子串的哈希值 = Hash[r + 1] - Hash[l] * base^(r - l + 1)。如:取出第二个 “bc”([4,5])的哈希值的操作为 Hash[5 + 1] - Hash[5 - 2 + 1] * 10^2。

#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const ull base = 10;//基数 应该取较大的质数,如:2333,避免冲突。
ull Hash[1000];
char s[] = "abcabcabc";
int main()
{
	int lens = strlen(s);
    // 计算 Hash 数组
    for(int i = 1; i <= lens; i++)
		Hash[i] = Hash[i - 1] * base + (s[i - 1] - 'a' + 1);
   	// 取出 [4,5] 子串的哈希值
    printf("%llu", Hash[5 + 1] - Hash[5 - 2 + 1] * 10 * 10);
    return 0;
}

字符串哈希可以在 o(1)的时间内得到某子串的哈希值,且该哈希值对于不同的字符串是不同的,因此可以遍历主串,求出与模式串长度一样的子串的哈希值再与模式串的哈希值做比较,若相等则说明该子串为模式串。

#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
char s[N], t[N];
int lens, lent;
typedef unsigned long long ull;
const ull base = 2333;//基数 

ull Hash[N], hash_t;
int main()
{
	scanf("%s%s", s, t);
	lens = strlen(s);
	lent = strlen(t);
	for(int i = 1; i <= lens; i++)
		Hash[i] = Hash[i - 1] * base + s[i - 1]; // unsigned long long 自然溢出,即自己取模
	// 计算模式串 t 的哈希值
	for(int i = 1; i <= lent; i++) 
		hash_t = hash_t * base + t[i - 1];
	int ans = 0;
	ull p = 1;
	for(int i = 1; i <= lent; i++)
		p *= base;
	for(int i = lent - 1; i < lens; i++)
	{
		if((Hash[i  + 1] - Hash[i - lent + 1] * p) == hash_t)
			ans++;
	}
	if(ans)	printf("YES\n%d\n", ans);
	else
		printf("NO\n");
	return 0;
}

解此题的过程中还遇到两个小问题:

​ 1 reference to ’ hash’ is ambiguous:此报错表示和库里面的属性或方法重名;

​ 2 double pow(double base,double exp)。unsigned long long 不可直接调函数计算。

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值