题干:

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.

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 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").

题目大意:

首先告诉你了两个定义:1.字符串的异构(其实就是当前串的某个排列),2.子字符串,3.good string。

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

解题报告:

这种字符串涉及排列的,,显然要计数一下的。因为包含p字符串中的所有字母及对应个数相同的话,那么就可以满足。类似这道题【CodeForces - 1038A 】Equality (思维水题,预处理字符串)。

实现的话滑窗法就好了呀,用一个长度为len2的窗口,从头滑到尾,顺便维护字符出现的个数和ans就好。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAX = 1e5 +5;
int bk[555],cnt[555],ans;
char s[MAX],p[MAX];
int main()
{cin>>(s+1);cin>>(p+1);
//  printf("%d",'?');int len1 = strlen(s+1);int len2 = strlen(p+1);if(len2 > len1) {puts("0");return 0;}for(int i = 1; i<=len2; i++) bk[p[i]]++;int l = 1,r = len2;for(int i = l; i<=r-1; i++) cnt[s[i]]++;while(r <= len1) {cnt[s[l-1]]--;cnt[s[r]]++;int rest = 0,flag = 1;for(int i = 'a'; i<='z'; i++) {if(cnt[i] > bk[i] ) {flag = 0;break;}if(cnt[i] < bk[i] ) rest +=  bk[i] - cnt[i];}if(flag && rest == cnt['?']) ans++;l++,r++;}printf("%d\n",ans);return 0 ;
}

总结:注意为了统一形式,while上面那个for只能处理到r-1,然后相当于l=1,r=len2再开始处理,不然就得特判一下。。

【CodeForces - 144C】Anagram Search(尺取,滑窗问题,处理字符串计数)相关推荐

  1. codeforces 293E Close Vertices 点分治+滑窗+treap

    http://codeforces.com/contest/293/problem/E 题意:求树上合法点对的个数.合法条件为:路径长度<=W,路径边数<=L. 显然是点分治.求解的时候第 ...

  2. The Best Vacation CodeForces - 1358D(贪心+尺取)

    You've been in love with Coronavirus-chan for a long time, but you didn't know where she lived until ...

  3. 【CodeForces - 660C】Hard Process (尺取 或 二分+滑窗,前缀和预处理)

    题干: You are given an array a with n elements. Each element of a is either 0 or 1. Let's denote the l ...

  4. Codeforces Round #736 (Div. 2) D. Integers Have Friends ST表gcd + 尺取

    传送门 文章目录 题意: 思路: 题意: 给你一个序列aaa,求一个最长的子序列[l,r][l,r][l,r]满足aimodm=ai+1modm=...=armodma_i\bmod m=a_{i+1 ...

  5. 【尺取或dp】codeforces C. An impassioned circulation of affection

    http://codeforces.com/contest/814/problem/C [题意] 给定一个长度为n的字符串s,一共有q个查询,每个查询给出一个数字m和一个字符ch,你的操作是可以改变字 ...

  6. Codeforces Round #321 (Div. 2) B. Kefa and Company (尺取)

    排序以后枚举尾部.尺取,头部单调,维护一下就好. 排序O(nlogn),枚举O(n) #include<bits/stdc++.h> using namespace std; typede ...

  7. CodeForces - 1358D The Best Vacation(前缀和+尺取)

    题目链接:点击查看 题目大意:给出 n 个数组成的数列,每个元素都可以展开为 1 , 2 , 3 .... a[ n ] ,现在将数列首尾相接,要求选取一段长度为 x 的连续数列,使得元素和最大 题目 ...

  8. CodeForces - 1333C Eugene and an array(尺取)

    题目链接:点击查看 题目大意:给出一个长度为 n 的数组 a,抛出 good 数组的定义: good 数组为数组 a 的一个子数组 good 数组的任意子数组之和均不为 0 (注意区分子数组和子数列的 ...

  9. CodeForces - 1198A MP3(尺取)

    题目链接:点击查看 题目大意:给出n个数字,表示不同的数据,现在我们需要对数据进行压缩,压缩的规则是: 现在给出一个I,表示内存大小为,n个数字中,相同的数据可以占用同一片内存,不同的数据必须占用不同 ...

最新文章

  1. Scala Implicit Conversion
  2. 生活点滴:java基础知识细化
  3. centos7离线安装mysql_Red Hat6.4离线安装mysql安装手册
  4. k8s service:ClusterIP、NodePort、LoadBalancer、ExternalName
  5. Android Json处理框架
  6. 2017-2018-1 20155223 实验三 实时系统
  7. go strconv
  8. c语言之计算两个数的大数
  9. 集合的交并差 -python
  10. 通过自研数据库画像工具支持“去O”数据库评估
  11. jsp如何使用kindeditor
  12. jwplayer 初始化设置项
  13. linux安装pyodbc模块,Linux下安装pyodbc报错 error: command 'gcc' failed with exit status 1
  14. python矩形法求定积分_如何用矩形法(梯形法)求定积分
  15. js 动态添加标签元素并赋值
  16. docker-ce 的安装与镜像加速
  17. win10计算机还原点如何创建,win10系统如何建立自动还原点?
  18. Arduino 使用 LCD1602 显示屏IIC驱动
  19. 英菲克I5M_I6M_I7M_I10M-晶晨S805处理器-当贝纯净桌面-线刷固件包
  20. mysql的主句与从句_什么是主句,什么是从句

热门文章

  1. [剑指offer]面试题第[36]题[JAVA][二叉搜索树与双向链表][递归]
  2. c语言三阶素数魔方阵,用C语言构造3*3素数魔方阵,即找出9个不大于500的素数并排成魔方阵。...
  3. linux上git克隆命令,Git clone命令用法
  4. bios设置开机双系统选择_打破专家的断言,突破微软和英特尔的封锁,惠普电脑玩转双系统...
  5. RT-Thread I2C设备驱动框架的对接使用
  6. linux脚本ls输出到变量中,bash – 将命令输出的错误消息存储到shell变量中
  7. c语言math函数 sgn,常用矩阵计算C语言代码
  8. 限定概率抽奖_守护星已点亮,内测皮肤得到没?从天美抽奖概率分析:地址什么梗...
  9. UE4打包后如何调试
  10. boost::weak_ptr和enable_shared_from_this