Codeforces-144C-Anagram Search(思维)
来源:互联网 发布:国家电网充电桩软件 编辑:程序博客网 时间:2024/06/06 05:02
A string t is called an anagram of the string s, if it is possible to rearrange letters int 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 strings. 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 stringp, consisting of lowercase Latin letters only. Let's assume that a string isgood if you can obtain an anagram of the stringp 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).
The first line is non-empty string s, consisting of no more than105 lowercase Latin letters and characters "?". The second line is non-empty stringp, consisting of no more than 105 lowercase Latin letters. Please note that the length of the stringp can exceed the length of the string s.
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.
bb??x???aab
2
ab?cacb
2
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").
思路:首先确定肯定输出为0的情况,当p串长度大于s串时或者p为空串时输出0;然后从s串的下标len2开始,每次维护它前len2个字符出现的次数(前缀和思想),然后判断的时候某一段字串‘?’的个数等于字串相对于p串缺少的字符个数,则ans++,如果子串中某个字符出现的次数大于p串中该字符出现的次数,是不符合要求的;具体请看代码。
AC代码:
#include<iostream>#include<cstring>#include<cstdio>#include<algorithm>using namespace std;struct node{ int cnt[27];}arr[100005];//前缀数组,?放在第26个int cnt[26];//记录p串中各个字符出现的次数char str1[100005],str2[1000006];int main(){ while(~scanf("%s%s",str1+1,str2+1)) { int len1=strlen(str1+1); int len2=strlen(str2+1); if(len2>len1||len2==0){//肯定为0的情况 printf("0\n"); continue; } memset(cnt,0,sizeof(cnt)); for(int i=0;i<=len1;i++) memset(arr[i].cnt,0,sizeof(arr[i].cnt)); for(int i=1;i<=len2;i++) cnt[str2[i]-'a']++; int ans=0; for(int i=1;i<=len2;i++)//先处理出第一项 { if(str1[i]=='?') arr[len2].cnt[26]++; else arr[len2].cnt[str1[i]-'a']++; } for(int i=len2+1;i<=len1;i++) { for(int j=0;j<=26;j++)//先继承上一个的 arr[i].cnt[j]=arr[i-1].cnt[j]; if(str1[i-len2]=='?') arr[i].cnt[26]--;//删掉最左边的 else arr[i].cnt[str1[i-len2]-'a']--; if(str1[i]=='?') arr[i].cnt[26]++;//加上当前的 else arr[i].cnt[str1[i]-'a']++; } for(int i=len2;i<=len1;i++) { int c=0,flag=1;; for(int j=0;j<=25;j++) { if(cnt[j]){ if(arr[i].cnt[j]<=cnt[j]) c+=(cnt[j]-arr[i].cnt[j]); else{ flag=0; break; } } } if(flag&&arr[i].cnt[26]==c) ans++; } printf("%d\n",ans); } return 0;}
- 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 723C】 + 思维
- codeforces C. Queue (思维)
- codeforces-750【C思维】
- Codeforces 842C(思维)
- codeforces 250/C 逆向思维
- Codeforces 651 C Watchmen【思维】
- codeforces-257【C思维,数学】
- codeforces-337【C思维,贪心】
- Codeforces 141C Queue【思维】
- 【codeforces 233C Cycles】+ 思维
- codeforces-757-【B、C思维】
- 内存问题的排查工具和方法– Clang的AddressSanitizer
- HTML超文本语言入门(一)
- ubuntu 16.04 LTS sublime Text 3 中文
- Python format() 函数
- 关于解决"No suitable driver found for jdbc:mysql:/localhost:3306"问题的步骤
- Codeforces-144C-Anagram Search(思维)
- Retrofit+Rxjava+Okhttp+MVP
- 【递推+lucas定理】BZOJ2111 [ZJOI2010]Perm 排列计数
- python之sqlalchemy使用
- 布尔类型、引用
- Codeforces Gym-101617F Move Away [计算几何]
- 【java day4】 数组 Array,最大/小值,进制转换
- 自定义Adapter实现RecyclerView的可展开二级列表expand效果
- 数字与静态(HeadfirstJava随笔)(转)