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

题干:

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再开始处理,不然就得特判一下。。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441481.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

互补品的交叉弹性系数_重庆事业单位综合备考:需求价格弹性和需求交叉价格弹性有何区别...

需求价格弹性 VS 需求交叉价格弹性一、需求价格弹性指某种商品随着价格的变化&#xff0c;该商品需求变化的程度大小(这里指的是同一种商品)。或者说&#xff0c;是需求变化相对于价格变化的敏感程度。假设&#xff1a;黄金、IPONE手机等商品降价50%&#xff0c;一定会大卖&…

esxi服务器与虚拟机时间不符,vsphere6.7-虚拟机与ESXI时间同步

ELF Format 笔记(十五)—— 符号哈希表ilocker:关注 Android 安全(新手) QQ: 2597294287 符号哈希表用于支援符号表的访问,能够提高符号搜索速度. 下表用于解释该哈希表的组织,但该格式并不属于 ELF 规范. ...AD域修改组策略如果我们的计算机加入AD域之后,修改安全策略时不能用…

朋友圈自动回复评论_微信新版,朋友圈可以表情包回复了!网友:评论区斗起来.jpg...

你们发现了吗&#xff1f;朋友圈可以发表情包评论了&#xff01;微信iOS版7.0.9正式版今天迎来更新支持发消息时可以引用之前的内容更令人惊喜的是不少网友都发现新版本还新增朋友圈自定义表情评论功能可以用表情包评论别人的朋友圈内容了&#xff01;轻触表情包还可以查看大图…

【CodeForces - 144D】Missile Silos(单源最短路,枚举中间边,枚举情况可能性)

题干&#xff1a; A country called Berland consists of n cities, numbered with integer numbers from 1to n. Some of them are connected by bidirectional roads. Each road has some length. There is a path from each city to any other one by these roads. Accordi…

第九大陆服务器未找到文件,第九大陆服务器优化规则说明 拍卖行和寄售功能关闭公告...

第九大陆已经确定了六月份服务器的优化时间与规则&#xff0c;同时为了配合6月8日的服务器优化实施&#xff0c;拍卖行将于6月5日关闭&#xff0c;待服务器优化完成后即可开放&#xff01;给大家带来的不便&#xff0c;敬请您谅解&#xff01;《第九大陆》服务器优化详细规则说…

【牛客 - 210A】游戏(思维,脑洞)

题干&#xff1a; BLUESKY007,fengxunling和dreagonm三个人发现了一个像素游戏,这款神奇的游戏每次会生成一个nxm的网格,其中每一个格子都被随机染色为R,G,B三种颜色之一,每次都可以选择任意一个非B颜色的格子进行一次操作,每次操作都会满足以下规则&#xff1a; 1.操作的范围为…

建房子 最安全图纸_妄想山海初期该怎么办?砍树狩猎建房子,还能拆别人的房子...

妄想山海还是一个很有趣的游戏&#xff0c;唯一的缺点就是游戏对手机配置要求比较高&#xff0c;手机太差的玩家&#xff0c;就别想那么多了&#xff0c;希望等游戏正式上线&#xff0c;能优化的好一些吧&#xff01;在测试中&#xff0c;玩家能体验到游戏中的各种玩法&#xf…

【POJ - 2226】Muddy Fields(匈牙利算法 或 网络流dinic,二分图匹配,最小点覆盖,矩阵中优秀的建图方式 )

题干&#xff1a; Rain has pummeled the cows field, a rectangular grid of R rows and C columns (1 < R < 50, 1 < C < 50). While good for the grass, the rain makes some patches of bare earth quite muddy. The cows, being meticulous grazers, dont w…

springboot的原生cache_springboot-shiro-redis-session-cache

1.配置mysql和redis redis的需要配置密码 原生jedis的使用相对于spring-data-redis更加灵活 所以本项目用原生jedis来直接作为缓存使用 具体使用方法请参看相关配置 特别注意如果mysql是5.7以上版本&#xff0c;请去掉sql_mode中的ONLY_FULL_GROUP_BY 本项目用户支持多角色多部…

【51Nod - 1272 】最大距离 (思维,排序sort的空间优化)

题干&#xff1a; 给出一个长度为N的整数数组A&#xff0c;对于每一个数组元素&#xff0c;如果他后面存在大于等于该元素的数&#xff0c;则这两个数可以组成一对。每个元素和自己也可以组成一对。例如&#xff1a;{5, 3, 6, 3, 4, 2}&#xff0c;可以组成11对&#xff0c;如…

javaweb在线问卷系统_2020 最新流行的Java Web报表工具比对

随着信息系统的高速发展&#xff0c;报表平台逐渐成为了信息系统当中最为核心和重要的功能模块。报表工具有助于将原始数据可视化显示&#xff0c;使决策者或者相关人员能够一览整体的数据趋势&#xff0c;完整的报表解决方案会提供多样的表格数据展示、数据可视化元素&#xf…

python 深copy_python中的深copy和浅copy

bytesPython bytes/strbytes 在Python3中作为一种单独的数据类型&#xff0c;不能拼接&#xff0c;不能拼接&#xff0c;不能拼接>>> €20.encode(utf-8)b\xe2\x82\xac20>>> b\xe2\x82\xac20.decode(utf-8)€20解码>>> b\xa420.decode(windows-1255…

【HDU - 4185】Oil Skimming (二分图,建图,匈牙利算法)

题干&#xff1a; Thanks to a certain "green" resources company, there is a new profitable industry of oil skimming. There are large slicks of crude oil floating in the Gulf of Mexico just waiting to be scooped up by enterprising oil barons. One …

python pandas read_csv 迭代器使用方法_pandas.read_csv参数详解(小结)

更多python教程请到友情连接&#xff1a; 菜鸟教程www.piaodoo.com人人影视www.sfkyty.com飞卢小说网www.591319.com韩剧网www.op-kg.com兴化论坛www.yimoge.cn星辰影院www.hhsos.netpandas.read_csv参数整理读取CSV(逗号分割)文件到DataFrame也支持文件的部分导入和选择迭代参…

【HDU - 2398 】Savings Account (水题模拟)

题干&#xff1a; Suppose you open a savings account with a certain initial balance. You will not make any withdrawals or further deposits for a number of years. The bank will compound your balance (add the annual interest) once a year, on the anniversary …

隧道凿岩机器人_隧道凿岩机器人的研制

隧道凿岩机器人的研制隧道、洞室开挖是现代交通、能源、采掘、建筑等大规模基本建设中的一项难度大、耗资耗时多、劳动条件差但又十分重要、十分关键的施工作业。早期的液压凿岩设备全都是由人工操作的液压凿岩钻车&#xff0c;其施工效率和施工精度完全取决于操作人员的熟练程…

【HDU - 1045】Fire Net (dfs 或二分图)

题干&#xff1a; Suppose that we have a square city with straight streets. A map of a city is a square board with n rows and n columns, each representing a street or a piece of wall. A blockhouse is a small castle that has four openings through which to …

html 按钮 按下 状态_第一次按下是启动,第二次按下是停止,俵哥分享2种接线方法...

朋友们大家好我是大俵哥&#xff0c;今天我们来说一下单按钮启停电路。这个电路虽然应用的不多&#xff0c;但是非常的经典&#xff0c;新手朋友们可以拿来练手。今天我们讲2种控制方法&#xff0c;一种用中间继电器控制一种用时间继电器控制&#xff0c;在看电路之前&#xff…

【 CodeForces - 1060B 】Maximum Sum of Digits(思维,构造)

题干&#xff1a; You are given a positive integer nn. Let S(x)S(x) be sum of digits in base 10 representation of xx, for example, S(123)1236S(123)1236, S(0)0S(0)0. Your task is to find two integers a,ba,b, such that 0≤a,b≤n0≤a,b≤n, abnabn and S(a)S(…

mysql 异步_MySQL -- 异步I/O

linux上&#xff0c;innodb使用异步IO子系统(native AIO)来对数据文件页进行预读和写请求。行为受到参数innodb_use_native_aio控制。默认是开启的&#xff0c;且只是适用于linux平台&#xff0c;需要libaio库。在其他的类unix平台上&#xff0c;innodb使用的是同步I/O。由于历…