8-2 War of the Corporations

原题
题目大意:就是将第一个串的某些字符和谐掉使第二个串在第一个串中找不到,求和谐字符的最小的数字。
思路:我们先来看一个例子:
如果长串是efcacacaef短串是caca,我们发现caca在长串中出现了两次,并且是重叠的,于是我们可以采用贪心的思路,每次和谐最后的那一个字符,这样就能满足最小的条件了。下面我们就可以来打码了。

#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<numeric>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
using namespace std;
int main()
{
    string a,b;
    cin>>a>>b;
    int res=0;
    int last=-1;
    int num1=(int)a.size();
    int num2=(int)b.size();
    for(int i=0;i<=num1-num2;++i)
    {
        string tt=a.substr(i,num2);
        if(tt==b)
        {
            if(last>=i)
                continue;
            else
            {
                ++res;
                last=i+num2-1;
            }
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Liusyu6688/article/details/85028611
war