原题点这里 Time Limit: 1000MS Memory Limit: 65536K
Description
Many people like to solve hard puzzles some of which may lead them to madness. One such puzzle could be finding a hidden prime number in a given text. Such number could be the number of different substrings of a given size that exist in the text. As you soon will discover, you really need the help of a computer and a good algorithm to solve such a puzzle.
Your task is to write a program that given the size, N, of the substring, the number of different characters that may occur in the text, NC, and the text itself, determines the number of different substrings of size N that appear in the text.
As an example, consider N=3, NC=4 and the text “daababac”. The different substrings of size 3 that can be found in this text are: “daa”; “aab”; “aba”; “bab”; “bac”. Therefore, the answer should be 5.
Input
The first line of input consists of two numbers, N and NC, separated by exactly one space. This is followed by the text where the search takes place. You may assume that the maximum number of substrings formed by the possible set of characters does not exceed 16 Millions.
Output
The program should output just an integer corresponding to the number of different substrings of size N found in the given text.
Sample Input
3 4
daababac
Sample Output
5
题目:
在一个长度不超过16000000的字符串中,只会出现NC个字符。求有多少个长度为N的不同子串
思路
这道题如果按照正常的字符串hash是绝对会TLE的
(131什么的就别想了,一定要记得优化啊)
对字符串hash的优化就是,重新定义各个字母的编号,而不是直接用ASCII码值(废话)
这时候我们就要巧用NC这个变量
(下面贴出的代码里面是直接比NC+1,但最好取比NC大的最小素数,才能完全避免重复的hash值)
知道这一点之后就方便了许多
然后本来是用set的,但还是超时(所以STL有时并不比手打的东西快啊)
最后只好自己写了个hash数组(当然还是很好打的)
时间复杂度(n*m)又因为m小于26所以复杂度并不高
代码
#include<cstring>
#include<cstdio>
using namespace std;
int num[300],n,m;
char a[16000005];
bool hash[16000005];//hash数组
int main()
{
scanf("%d%d%s",&n,&m,a);
int lens=strlen(a);
int t=0;
num[a[0]]=t++;
for(int i=1;i<lens;i++)
{
if(num[a[i]]==0)
num[a[i]]=t++; //重新为出现的字母编号
} //最后的t比nc(即m)多1
int ans=0;
for(int i=0;i<=lens-n;i++)
{
int sum=0;
for(int j=0;j<n;j++)
{
sum=sum*t+num[a[i+j]];//说白了直接用t就是为了偷懒
}//计算每个数的hash值
if(!hash[sum])
{
ans++;
hash[sum]=true;
}//如果这个数没有出现过那么就存起来
}
printf("%d\n",ans);
return 0;
}