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 p can 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”).

题目链接
参考题解

题目要求找第二个字符串能形成第一个字符串的连续子串的个数。
这个题目看大佬好像用了什么哈希算法,记录第二个字符串中每一个小写字母出现的次数,然后再对第一个字符串进行遍历,到达第二个字符串的长度之后就判断,是不是每一个第一个的连续子串中出现的小写字母次数都小于第二个,因为有问号的存在,所以小于的话是完全可以的,但是如果大于一定是无法匹配的,因为第二个字符串无法提供那么多该字母。具体还是看代码:
AC代码:

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e5 + 5;
char str_1[maxn], str_2[maxn];  //记录两个字符串
int letter_1[26], letter_2[26]; //记录两个字符串中每一个字符出现的次数

int main()
{
    int ans, len_1, len_2;
    bool flag;
    while(~scanf("%s%s", str_1, str_2))
    {
        //初始化letter为0
        memset(letter_1, 0, sizeof(letter_1));
        memset(letter_2, 0, sizeof(letter_2));
        len_1 = strlen(str_1), len_2 = strlen(str_2);
        ans = 0;
        //先将第二个字符串中出现字符的数量统计下来
        for(int i = 0; i < len_2; i++)  letter_2[str_2[i] - 'a']++;
        for(int i = 0; i < len_1; i++)
        {
            flag = true;
            letter_1[str_1[i] - 'a']++;
            if(i < len_2 - 1)   continue ;
            for(int j = 0; j < 26; j++)
                if(letter_1[j] > letter_2[j])   //如果有一个字符比第二个多的话,那么一定不匹配
                {
                    flag = false;
                    break ;
                }
            if(flag)    ans++;
            letter_1[str_1[i - (len_2 - 1)] - 'a']--;   //头上的字符数量减去一
        }
        printf("%d\n", ans);
    }
    return 0;
}

### 哈希表算法练习题及相关资源 哈希表是一种非常高效的数据结构,在实际应用中广泛用于解决查找、存储等问题。为了更好地掌握哈希表及其相关算法,可以通过以下途径获取高质量的练习题: #### 推荐的经典书籍 对于初学者来说,《算法导论》[^3] 是一本不可多得的好书,它不仅详细介绍了哈希表的工作原理,还深入探讨了其性能优化方法以及冲突处理策略。另一本书籍《数据结构与算法分析》也提供了丰富的实例和理论支持,帮助读者理解如何设计高效的哈希函数。 #### 在线平台上的优质题目 以下是几个适合刷题并巩固哈希表知识的在线编程平台: - **LeetCode**: LeetCode 提供了许多经典的哈希表问题,例如两数之和(Two Sum)、有效的字母异位词(Valid Anagram),以及四数相加 II(4Sum II)。这些问题覆盖了从简单到困难的不同难度级别[^1]。 示例代码实现如下所示: ```python def two_sum(nums, target): hash_map = {} for i, num in enumerate(nums): complement = target - num if complement in hash_map: return [hash_map[complement], i] hash_map[num] = i return [] ``` - **GeeksforGeeks**: 这是一个专注于计算机科学基础知识的教学网站,其中关于哈希表的部分包含了大量详细的教程和实践案例。比如,“Find the first non-repeating character from a stream of characters” 就是一道典型的基于哈希表的应用型题目。 - **Codeforces & AtCoder**: 如果希望挑战更高阶的内容,则可以尝试 Codeforces 或者 AtCoder 上的比赛真题。这类平台上经常会出现涉及高级哈希技巧的问题,如字符串匹配中的 Rabin-Karp 算法等[^4]。 #### 自定义学习路径建议 当准备面试或者提升个人技能时,应该遵循循序渐进的原则来进行训练。先熟悉基础操作,然后再逐步过渡到更复杂的场景模拟。具体而言就是按照以下顺序展开复习计划——先阅读权威资料了解核心知识点;接着动手完成若干入门级项目验证所学成果;最后参与真实环境下的编码测试进一步强化记忆效果[^2]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值