CodeForces 144C Anagram Search(思维)

C. Anagram Search

time limit per test

2 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 ispossible to rearrange letters in t so that it is identical to the strings. 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 canbe read starting from some position in the string s. For example, thestring "aba" has six substrings: "a", "b", "a","ab", "ba", "aba".

You are given a string s, consisting of lowercase Latin letters andcharacters "?". You are also given a string p, consisting oflowercase Latin letters only. Let's assume that a string is good if you canobtain an anagram of the string p from it, replacing the "?"characters by Latin letters. Each "?" can be replaced by exactly onecharacter 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 105lowercase Latin letters and characters "?". The second line isnon-empty string p, consisting of no more than 105 lowercaseLatin letters. Please note that the length of the string p can exceedthe length of the string s.

Output

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

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

Examples

Input

bb??x???
aab

Output

2

Input

ab?c
acb

Output

2

Note

Consider the first sample test. Here the string s has two goodsubstrings: "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 twogood substrings: "ab?" ("?" can be replaced by "c"),"b?c" ("?" can be replaced by "a").

 

 

题目大意:

         给你两个字符串,如果s的子字符串中包含p字符串中的所有字母,就可以说这个子字符串是符合条件的。S字符串有小写字母和?组成,而?号可以替换为任意的字母。最后问你,有多少个符合条件的子字符串。(子字符串是连续的若干个字符)

 

解题思路:

         字符串的长度太长,暴力扫描一遍是O(N^2)的复杂度,会超时。

我们判断的思路,也是判断字母个数是否相等。那我们用两个数组记录字符串中一段字符的字母个数,用这两个表比较。往后移动的时候,把之前的那个字母去掉以后在往后移动就可以实现遍历s的所有子字符串。这样就大幅降低复杂度。只用扫描一遍s字符串即可


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<functional>
using namespace std;

char a[100005],b[100005];
int cnta[30],cntb[30];
int main()
{
	int i,j;
	int lena,lenb;
	int ans,flag;
	ans=0;
	ans=0;
	memset(cnta,0,sizeof cnta);
	memset(cntb,0,sizeof cntb);
	gets(a); gets(b);
	lena=strlen(a);
	lenb=strlen(b);
	for(i=0;i<lena;i++)//统计p中字符串的字母个数
		cntb[b[i]-'a' ]++;
	for(i=0;i<lena;i++)
	{
		flag=1;
		cnta[a[i]-'a' ]++;//统计s中字符串的字母个数
		if(i+1>=lenb)
		{
			for(j=0;j<26;j++)
			{
				if(cnta[j]>cntb[j])//比较字母个数
				{
					flag=0;//只有大于的时候退出
					break;//小于等于的时候有?号来凑
				}
			}
			if(flag)
				ans++;
			cnta[a[i-(lenb-1)]-'a']--;//减去最前面的字母,往后移动
		}
	}
	printf("%d\n",ans);
}


 


全部评论

相关推荐

11-02 09:49
已编辑
货拉拉_测试(实习员工)
热爱生活的仰泳鲈鱼求你们别卷了:没事楼主,有反转查看图片
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务