poj 1200 Crazy Search(疯狂搜索)

来源:互联网 发布:flower dance知乎 编辑:程序博客网 时间:2024/05/21 10:53

  • 题目
  • 题解
  • 代码

题目

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.

意思是:有一个字符串总共包含NC种字符,求其长度为N的子串(不包含重复的)的数量。

也可以看成:给你n和nc,让你把所有n位的字母转换成nc进制的数,问这些数有多少个(不能重复)。

题解

题目尤难懂也!有道翻译者,亦不解其意。何也?乃众里寻他千百度,卒解其意。

两个写C的题解

http://blog.csdn.net/u014733623/article/details/26388067

http://www.cnblogs.com/lzmfywz/p/3161199.html
用Hash表,把字符串转为数字来储存

样例输入
3 4
daababac

样例输出
5

好不容易打完,然而发现————————
17599136 yjy_aii 1200 Compile Error Pascal 595B 2017-09-09 07:43:04
17599119 yjy_aii 1200 Compile Error Pascal 625B 2017-09-09 07:31:29
17599117 yjy_aii 1200 Compile Error Pascal 619B 2017-09-09 07:26:20
17599110 yjy_aii 1200 Compile Error Pascal 624B 2017-09-09 07:23:03

Why?
原来P已经落伍了,pku所有提交P的都编译错误了

于是可怜的我,可怜的零C++基础的我,脑子空空,两眼茫然,开始了P转C的进程……

代码

原来打的P

var  n,nc,len,i,j,k,t,ans:longint;  s:ansistring;  b:array[0..300]of longint;  h:array[0..10000007]of longint;function locate(c:longint):longint;begin  if h[c]=0 then begin h[c]:=1;inc(ans);end;end;begin  readln(n,nc);  readln(s);  len:=length(s);  for i:=1 to len do    begin      if b[ord(s[i])]=0 then        begin          inc(k);          b[ord(s[i])]:=k;        end;      if k>=nc then break;    end;  for i:=1 to len-2 do    begin      t:=0;      for j:=i to i+n-1 do        t:=(t*10+b[ord(s[j])])mod 10000007;      locate(t);    end;  writeln(ans);end.

历经千辛万苦终于写完了

#include<iostream>#include<string>#include<cstdio>using namespace std;const int q=10000007;string str;int h[q];int b[500];int main(){    int t,e,n,nc,k=0,ans=0;    scanf("%d%d",&n,&nc);    cin>>str;    for (int i=0;i<str.length();i++)    {         if (b[str[i]]==0) b[str[i]]=++k;         if (k==nc) break;    }    for (int i=0;i<str.length()-n+1;i++)    {         t=0;         for (int j=i;j<i+n;j++)            t=(t*nc+b[str[j]])% q;         if (h[t]==0)          {             h[t]=1;             ans++;         }    }    printf("%d",ans);    return(0);}