HDU4847(字符串匹配)

http://acm.hdu.edu.cn/showproblem.php?pid=4847

题意:

给定一段文本,统计其中doge出现的次数

思路:

水题,kmp可以做,使用STL中的string更简单;

代码:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<iomanip>
#include<algorithm>
#include<string.h>
#include<queue>
#include<cmath>
#include<stack>

using namespace std;
const int maxn=3e5+10;
const int inf=0x7f7f7f7f;
typedef long long ll;

string doge;
string ma="doge";
int main()
{
    ll ans=0;
    while(getline(cin,doge)){
        for(int i=0; i<doge.length(); i++){
            if(doge[i]<='Z'&&doge[i]>='A')
             doge[i]=doge[i]-'A'+'a';
        }
        ll pos=0;
        while(pos!=string::npos)
        {
            pos=doge.find(ma,pos);
            if(pos!=string::npos){
                ans++;
                pos+=4;
            }
        }
    }
    cout<<ans<<endl;
    system("pause");
    return 0;
}

 

posted on 2021-02-01 11:53  mmn  阅读(77)  评论(0编辑  收藏  举报