zoukankan      html  css  js  c++  java
  • Codeforces Round #342 (Div. 2) B. War of the Corporations 贪心

    B. War of the Corporations

    题目连接:

    http://www.codeforces.com/contest/625/problem/B

    Description

    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 Input

    intellect
    tell

    Sample Output

    1

    Hint

    题意

    给你s1 s2,你每次操作可以使得s1串中某个字符变成#,然后问你最小修改多少次,就可以使得s1中不含有s2子串

    题解:

    贪心,我们每次修改最后一个字符就好了

    这样相交的一定都被修改了

    代码

    #include<bits/stdc++.h>
    using namespace std;
    
    string s1,s2;
    int main()
    {
        cin>>s1>>s2;
        int ans = 0;
        for(int i=0;i<s1.size();i++)
        {
            int flag = 0;
            for(int j=0;j<s2.size();j++)
            {
                if(i+j>s1.size())
                {
                    flag = 1;
                    break;
                }
                if(s1[i+j]!=s2[j])
                {
                    flag = 1;
                    break;
                }
            }
            if(flag==0)
            {
                s1[i+s2.size()-1]='#';
                ans++;
            }
        }
        cout<<ans<<endl;
    }
  • 相关阅读:
    新能源汽车产业发展规划(2021-2035年)
    Spire.Doc 生成长图
    【Kafka】单机Kafka测试
    【Linux】IO多路复用之select、poll、epoll模型
    【Linux】五种IO模型
    【Linux】零拷贝技术(Zero Copy)
    【Linux】系统相关概念
    【SpringBoot】SpringBoot 整合Redisson
    【Spring】SpringBoot + SpringSession + Redis 实现Session共享
    【SpringBoot】SpringBoot 整合Redis
  • 原文地址:https://www.cnblogs.com/qscqesze/p/5184921.html
Copyright © 2011-2022 走看看