CodeForces 144C Anagram Search(思维)

本文介绍了一种高效的子串匹配算法,用于寻找字符串s中符合特定条件的子串数量。s字符串由小写字母和问号组成,问号可替换成任意字母。通过优化算法避免了O(N^2)的时间复杂度,确保了处理大型数据集的可行性。

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);
}


 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值