CodeForces 144C Anagram Search

来源:互联网 发布:维多利亚服饰知乎 编辑:程序博客网 时间:2024/05/22 00:10

 

Anagram Search

time limit per test2 seconds

memory limit per test
256 megabytes
input
standard input
output
standard output

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 p can 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.

Sample test(s)
input
bb??x???aab
output
2
input
ab?cacb
output
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").

题意不多说,坑比AC代码,,

#include <iostream>#include <cstdio>#include <cstring>#define MAXN 100005using namespace std;char a[MAXN], b[MAXN];int hasha[26], hashb[26];int main()  //哈希算法原理{int ans, lena, lenb;bool flag;while (scanf("%s%s", a, b) != EOF){memset(hasha, 0, sizeof(hasha));memset(hashb, 0, sizeof(hashb));lena = strlen(a);lenb = strlen(b);ans = 0;for (int i = 0; i < lenb; ++i)hashb[b[i] - 'a']++;for (int i = 0; i < lena; ++i){flag = true;hasha[a[i] - 'a']++;//记录字符串a中字母个数   if (i >= lenb - 1){for (int j = 0; j < 26; ++j){if (hasha[j] > hashb[j]){flag = false;//如果a中的字符串有比b中多的字母,肯定不匹配   break;}}if (flag == true)ans++;hasha[a[i - (lenb - 1)] - 'a']--;//把“最前面”那个字符个数去掉   }}printf("%d\n", ans);}return 0;}

 

 

0 0
原创粉丝点击