CodeForces 144C

问题描述:

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 10^5 lowercase Latin letters and characters "?". The second line is non-empty string p, consisting of no more than 10^5 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

bb??x???
aab
2

ab?c
acb
2

题目题意:题目给了我们俩个字符串,问s串中有多少个子串和p串相等(俩个字符串相等,只要保证俩个串的字符种类和个数都相等,其中'?'可以替换为任意字符.

题目分析:我们把p字符串处理一下,用一个数组a[26]保存起来,每一位保存相应的字符个数。首先去匹配s串长度为p串长度的字符串,记录第一个字符,然后枚举每一个s串的字符,每移一位就把前面的字符丢掉,把后面的加进来。对于'?'记录它的个数就好了。

代码如下:

#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<cstring>
using namespace std;

int a[30];
int check()//判断是否缺少字符,如果多了肯定不行
{
    int ans=0;
    for (int i=1;i<=26;i++) {
        if (a[i]<0) return -1;
        else ans+=a[i];
    }
    return ans;
}
int main()
{
    string s,p;
    while (cin>>s>>p) {
        memset (a,0,sizeof (a));
        if (p.size()>s.size()) {
            printf("0");
            continue;
        }
        for (int i=0;i<p.size();i++) {
            a[p[i]-'a'+1]++;
        }
        int res=0,ans=0;
        for (int i=0;i<p.size();i++) {
            if (s[i]=='?') res++;
            else a[s[i]-'a'+1]--;
        }
        char first=s[0];
        if (check()==res) ans++;
        for (int i=0;i<s.size();i++) {
            if (i+p.size()>=s.size()) break;
            else {
                if (first=='?') res--;
                else a[first-'a'+1]++;
                if (s[i+p.size()]=='?') res++;
                else a[s[i+p.size()]-'a'+1]--;
                if (check()==res) ans++;//缺少字符与'?'相等
                first=s[i+1];
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}










  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值