CodeForces 144C Anagram Search
来源:互联网 发布:维多利亚服饰知乎 编辑:程序博客网 时间:2024/05/22 00:10
Anagram Search
time limit per test2 seconds
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").
题意不多说,坑比AC代码,,
#include <iostream>#include <cstdio>#include <cstring>#define MAXN 100005using 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;}
- 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 144CAnagram Search
- codeforces-729C-bindary search,math
- [leetcode]Valid Anagram C语言
- 【LeetCode-242】Valid Anagram(C++)
- leetcode_242_Valid Anagram(easy)(C++)(Java)
- 242. Valid Anagram(C++)
- anagram
- Anagram
- Anagram
- Anagram
- LeetCode之Valid Anagram (Java+C/C++)
- java 泛型学习笔记
- [Leetcode] 41. First Missing Positive
- 写点东西
- Eclipse保存密码时发生异常的解决方法
- connect函数
- CodeForces 144C Anagram Search
- Tachyon源码结构分析(二)
- 面向模型
- Oracle 11gR2手工复制
- 基于Eclipse OEPE搭建OSB开发环境
- Android 线程 Looper.prepare()、Looper.loop() 使用
- 第二周上机实践项目——课后实践(1)
- Java IO操作
- badboy+jmeter