CodeForces 144C Anagram Search(暴力模拟)

超、凢脫俗 2022-06-04 05:43 301阅读 0赞

A string t is called an anagram of the string s, if it is possible to rearrange letters in t so that it is identical to the string s. For example, the string “aab” is an anagram of the string “aba” and the string “aaa” is not.

The string t is called a substring of the string s if it can be read starting from some position in the string s. For example, the string “aba” has six substrings: “a”, “b”, “a”, “ab”, “ba”, “aba”.

You are given a string s, consisting of lowercase Latin letters and characters “?”. You are also given a string p, consisting of lowercase Latin letters only. Let’s assume that a string is good if you can obtain an anagram of the string p from it, replacing the “?” characters by Latin letters. Each “?” can be replaced by exactly one character of the Latin alphabet. For example, if the string p = «aba», then the string “a??” is good, and the string «?bc» is not.

Your task is to find the number of good substrings of the string s (identical substrings must be counted in the answer several times).

Input

The first line is non-empty string s, consisting of no more than 105 lowercase Latin letters and characters “?”. The second line is non-empty string p, consisting of no more than 105 lowercase Latin letters. Please note that the length of the string pcan exceed the length of the string s.

Output

Print the single number representing the number of good substrings of string s.

Two substrings are considered different in their positions of occurrence are different. Thus, if some string occurs several times, then it should be counted the same number of times.

Example

Input

  1. bb??x???
  2. aab

Output

  1. 2

Input

  1. ab?c
  2. acb

Output

  1. 2

Note

Consider the first sample test. Here the string s has two good substrings: “b??” (after we replace the question marks we get “baa”), “???” (after we replace the question marks we get “baa”).

Let’s consider the second sample test. Here the string s has two good substrings: “ab?” (“?” can be replaced by “c”), “b?c” (“?” can be replaced by “a”).

题解:

数据范围很小,时间复杂度1e5*26,妥妥得不超时

代码:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<stdlib.h>
  4. #include<queue>
  5. #include<stack>
  6. #include<math.h>
  7. #include<vector>
  8. #include<map>
  9. #include<set>
  10. #include<stdlib.h>
  11. #include<cmath>
  12. #include<string>
  13. #include<algorithm>
  14. #include<iostream>
  15. #include<stdio.h>
  16. using namespace std;
  17. #define ll long long
  18. int a[30];
  19. int b[30];
  20. int main()
  21. {
  22. char s1[100005],s2[100005];
  23. int i,j,d=0,num=0,ans=0,len1,len2,t;
  24. scanf("%s%s",s1,s2);
  25. memset(a,0,sizeof(a));
  26. memset(b,0,sizeof(b));
  27. for(i=0;s2[i];i++)
  28. {
  29. a[s2[i]-'a']++;
  30. }
  31. len1=strlen(s1);
  32. len2=strlen(s2);
  33. if(len1<len2)
  34. {
  35. printf("0\n");
  36. return 0;
  37. }
  38. t=0;
  39. for(i=0;i<len2;i++)
  40. {
  41. if(s1[i]=='?')
  42. d++;
  43. else
  44. b[s1[i]-'a']++;
  45. }
  46. for(j=0;j<26;j++)
  47. {
  48. t+=abs(a[j]-b[j]);
  49. }
  50. if(t<=d)
  51. num++;
  52. for(i=1;i<=len1-len2;i++)
  53. {
  54. if(s1[i-1]=='?')
  55. d--;
  56. else
  57. b[s1[i-1]-'a']--;
  58. if(s1[i+len2-1]=='?')
  59. d++;
  60. else
  61. b[s1[i+len2-1]-'a']++;
  62. t=0;
  63. for(j=0;j<26;j++)
  64. {
  65. t+=abs(a[j]-b[j]);
  66. }
  67. if(t<=d)
  68. {
  69. num++;
  70. }
  71. //printf("i=%d d=%d t=%d!!\n",i,d,t);
  72. }
  73. printf("%d\n",num);
  74. return 0;
  75. }

发表评论

表情:
评论列表 (有 0 条评论,301人围观)

还没有评论,来说两句吧...

相关阅读