Anagram Search -- CodeForces
来源:互联网 发布:森田玻尿酸乳液知乎 编辑:程序博客网 时间:2024/06/05 22:57
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).
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 p 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").
题意:
就是规定一个字符串把顺序打乱了,和原来的字符串是 anagram string关系。因此,这里只需要处理字符串,想法比较简单,利用哈希算法。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#define MAXN 100005
using namespace std;
char a[MAXN], b[MAXN];
int hasha[26], hashb[26];
int main()
{
int ans, lena, lenb;
bool flag;
while(scanf("%s%s", a, b) != EOF)
{
memset(hasha, 0, sizeof(hasha));
memset(hashb, 0, sizeof(hashb));
lena = strlen(a);
lenb = strlen(b);
ans = 0;
for(int i = 0; i < lenb; ++i)
hashb[b[i] - 'a']++;
for(int i = 0; i < lena; ++i)
{
flag=true;
hasha[a[i] - 'a']++;//记录字符串a中字母个数
if(i >= lenb - 1)
{
for(int j=0; j < 26; ++j)
{
if(hasha[j] > hashb[j])
{
flag=false;//如果a中的字符串有比b中多的字母,肯定不匹配
break;
}
}
if(flag == true)
ans++;
hasha[a[i - (lenb - 1)] - 'a']--;//把“最前面”那个字符个数去掉
}
}
printf("%d\n", ans);
}
return 0;
}
- Anagram Search -- CodeForces
- CodeForces 144C Anagram Search
- Codeforces-144C-Anagram Search(思维)
- CodeForces 144C Anagram Search(思维)
- Codeforces Round #103 (Div. 2)---C. Anagram Search
- Codeforces#103-C. Anagram Search-乱搞水题
- CodeForces 144C Anagram Search(暴力模拟)
- anagram
- Anagram
- Anagram
- Anagram
- CodeForces 144CAnagram Search
- codeforces 528D Fuzzy Search
- Codeforces 830A, Binary Search
- 【codeforces】528D. Fuzzy Search【FFT】
- codeforces-729C-bindary search,math
- codeforces 528D Fuzzy Search FFT
- CodeForces 528 D.Fuzzy Search(FFT)
- hdoj 1116 Play on Words 【并查集】+【欧拉路】
- JSON(二)
- Open the Java build path property page of project 'Speaker'
- 博客已迁移~~
- 关于导出函数与调用约定,C++ Builder 调用 VC++ 编译的DLL
- Anagram Search -- CodeForces
- 函数指针实现函数回调
- HDU 3333 Turing Tree(离线线段树)
- 二叉树的基本算法
- C++ union使用注意
- Standard Input and Output Redirection
- 创建一个Cocos2d-x 空项目
- block实现函数回调
- java在线预览txt、word、ppt、execel,pdf代码