Codeforces Round #166 (Div. 2)

终于回到实验室了。。。埋头切题。。。

寒假唯一场cf,前3个比赛时候做出来了,第四个,我理解错题意+不会字符串哈希+不会trie树 == 做不出来。。

D. Good Substrings

先贴一份 看别人写的字典树自己改编的字典树。。。太神奇了,字典树写的这么简洁。。。

 1 #include <cstdio>

 2 #include <cstring>

 3 #include <string>

 4 #include <iostream>

 5 using namespace std;

 6 char str[1501],o[27];

 7 int trie[1500*1500][26];

 8 int ans,num;

 9 int main()

10 {

11     int i,j,k,len,t;

12     scanf("%s%s%d",str,o,&k);

13     len = strlen(str);

14     ans = 0;

15     for(i = 0; i < len; i ++)

16     {

17         t = 0;num = 0;

18         for(j = i; j < len; j ++)

19         {

20             if(o[str[j] - 'a'] == '0')

21                 t ++;

22             if(t > k) break;

23             if(!trie[num][str[j]-'a'])

24             {

25                 ans ++;

26                 trie[num][str[j]-'a'] = ans;

27                 num = ans;

28             }

29             else

30             {

31                 num = trie[num][str[j]-'a'];

32             }

33         }

34     }

35     printf("%d\n",ans);

36     return 0;

37 }

看网上大神有写字符串哈希的。。一样很神奇,很简洁。。。看这个两个链接:

http://blog.csdn.net/acm_cxlove/article/details/8579542

http://www.cnblogs.com/Delostik/archive/2013/02/14/2911197.html

 

你可能感兴趣的:(codeforces)