CodeForces 144C Anagram Search(思维)
来源:互联网 发布:华为网络管理平台价格 编辑:程序博客网 时间:2024/06/06 05:25
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 int 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 strings. 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 stringp, consisting oflowercase Latin letters only. Let's assume that a string is good if you canobtain an anagram of the stringp from it, replacing the "?"characters by Latin letters. Each "?" can be replaced by exactly onecharacter of the Latin alphabet. For example, if the stringp = «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 stringp, consisting of no more than 105 lowercaseLatin letters. Please note that the length of the stringp can exceedthe length of the string s.
Output
Print the single number representing the number of good substrings ofstrings.
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);}
- CodeForces 144C Anagram Search(思维)
- Codeforces-144C-Anagram Search(思维)
- CodeForces 144C Anagram Search
- CodeForces 144C Anagram Search(暴力模拟)
- Anagram Search -- CodeForces
- Codeforces Round #103 (Div. 2)---C. Anagram Search
- Codeforces#103-C. Anagram Search-乱搞水题
- CodeForces 822C(思维)
- codeforces 673C(思维)
- Codeforces #591 C Median Smoothing(思维)
- Codeforces--675C--Money Transfers(思维)
- 【CodeForces】337C - Quiz(贪心,思维)
- CodeForces 233C Cycles(思维)
- 【Codeforces-402C】-Dishonest Sellers(思维)
- CodeForces 828C String Reconstruction(思维)
- CodeForces 160C Find Pair(思维)
- 【codeforces 723C】 + 思维
- codeforces C. Queue (思维)
- 计算机网络系列(10)之应用场景Peer-to-peer systems
- 【PHP自学笔记】第十一章 图形图像处理
- MyBatis分页插件-PageHelper的配置与应用(二):即插即用的PageInfo<T>
- Sony Catalyst Production Suite 2017 官方版下载附安装教程
- 【转载】C#调用python文件时出现'module' object has no attribute '_getframe'
- CodeForces 144C Anagram Search(思维)
- linux系统之cifs文件的存储(samba)
- Java 笔记 之 开发工具 Eclipse 的安装
- 大数据(三十六)机器学习【相关概念】
- oracle数据库中几个大对象数据类型
- Hive字符函数
- 吴恩达深度学习笔记(四)week3目标检测
- 使用LaTeX基本数学公式
- 指针变量的相关内容