Codeforces 625B War of the Corporations (字符串匹配题目)

本文介绍了一个字符串匹配的问题背景:两家公司因产品名称相似而产生的纠纷。随后详细解释了问题的输入输出要求,并提供了一段实现字符串匹配功能的示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. War of the Corporations
time limit per test 1 second
memory limit per test 256 megabytes
input standard input
output standard output
A long time ago, in a galaxy far far away two giant IT-corporations Pineapple and Gogol continue their fierce competition. Crucial moment is just around the corner: Gogol is ready to release it’s new tablet Lastus 3000.
This new device is equipped with specially designed artificial intelligence (AI). Employees of Pineapple did their best to postpone the release of Lastus 3000 as long as possible. Finally, they found out, that the name of the new artificial intelligence is similar to the name of the phone, that Pineapple released 200 years ago. As all rights on its name belong to Pineapple, they stand on changing the name of Gogol’s artificial intelligence.
Pineapple insists, that the name of their phone occurs in the name of AI as a substring. Because the name of technology was already printed on all devices, the Gogol’s director decided to replace some characters in AI name with “#”. As this operation is pretty expensive, you should find the minimum number of characters to replace with “#”, such that the name of AI doesn’t contain the name of the phone as a substring.
Substring is a continuous subsequence of a string.
Input
The first line of the input contains the name of AI designed by Gogol, its length doesn’t exceed 100 000 characters. Second line contains the name of the phone released by Pineapple 200 years ago, its length doesn’t exceed 30. Both string are non-empty and consist of only small English letters.
Output
Print the minimum number of characters that must be replaced with “#” in order to obtain that the name of the phone doesn’t occur in the name of AI as a substring.
Sample test(s)
input
intellect
tell
output
1
input
google
apple
output
0
input
sirisiri
sir
output
2
Note
In the first sample AI’s name may be replaced with “int#llect”.
In the second sample Gogol can just keep things as they are.
In the third sample one of the new possible names of AI may be “s#ris#ri”.

本题是一个找子串个数的水题,,,我在这里就不再进行吐槽了,,。。直接附上AC代码。。大家看一下吧。。。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
char a[100050];
char b[100];

int judge(int la,int lb)
{
    int s=0;
    for(int i=0;i<lb;i++)
    {
        if(a[la]==b[i])
        {
            s++;
            la++;
        }
        else
        {
            break;
        }
    }
    if(s==lb)
    {
        return 1;
    }
    else return 0;
}

int main()
{
    while(~scanf("%s%s",a,b))
    {
        int la=strlen(a);
        int lb=strlen(b);
        int sum=0;
        for(int i=0;i<la;i++)
        {
            if(a[i]==b[0])
            {
                int result=judge(i,lb);
                if(result==1)
                {
                    sum++;
                    i=i+lb-1;
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}
### C++ 字符串哈希练习 #### 一、字符串哈希基础概念 字符串哈希是一种将字符串映射为整数的技术,通过这种方式可以快速比较两个字符串是否相等或查找子串等问。通常情况下,会选取一个基数`base`来模拟多进制转换过程,并利用模运算防止数值溢出。 #### 二、经典例题解析 ##### AcWing 841. 字符串哈希[^1] 此作为一道典型的字符串哈希入门题目,主要考察如何构建并应用简单的字符串哈希函数处理给定问。对于长度较大的文本匹配场景尤为适用。 ```cpp const int N = ...; unsigned long long h[N], p[N]; // 初始化p数组, 计算以base为底的幂次方表 void init() { p[0] = 1; for (int i = 1; i < N; ++i) p[i] = p[i - 1] * base % mod; } // 获取区间[l,r]对应的hash值 unsigned long long get(int l, int r) { return (h[r] - h[l - 1] * p[r - l + 1]) % mod; } ``` 上述代码片段展示了基于前缀和的方式预处理字符串哈希值的方法,其中`mod`用于取余操作确保不会发生越界错误;而`get()`方法则实现了任意区间的哈希查询功能。 #### 三、实战演练建议 为了更好地掌握这一知识点,推荐尝试以下几类具有代表性的习: - **单模式串匹配**:如POJ 2774 DNA Sequence,这类题目往往涉及在一个较长的目标序列中定位某个特定模式串的位置。 - **多重模式串匹配**:例如HDU 3746 Caesar Cipher Plus,在此类挑战里可能需要同时考虑多个不同长度的模式串与目标串之间的关系。 - **最长公共前后缀/回文串检测**:像Codeforces Round #XXX Div. Y Problem Z这样的竞赛真也值得深入研究,它们能够很好地锻炼选手灵活运用字符串哈希技巧的能力。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值