然而沼跃鱼早就看穿了一切(字符串匹配的题目)

<div style="box-sizing: border-box; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px; line-height: 20px;"><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼(“marshtomp”,不区分大小写)。为了使句子不缺少成分,统一换成 “fjxmlhx” 。</p><h3 style="box-sizing: border-box; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 20px; margin-bottom: 10px; font-size: 24px;">输入</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">输入包括多行。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">每行是一个字符串,长度不超过200。</p><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">一行的末尾与下一行的开头没有关系。</p><h3 style="box-sizing: border-box; font-weight: 500; line-height: 1.1; color: inherit; margin-top: 20px; margin-bottom: 10px; font-size: 24px;">输出</h3><p style="box-sizing: border-box; margin-top: 0px; margin-bottom: 10px;">输出包含多行,为输入按照描述中变换的结果。</p></div><dt style="box-sizing: border-box; line-height: 1.42857143; font-weight: bold; font-size: 18px; margin: 10px 0px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;">样例输入</dt><dd style="box-sizing: border-box; line-height: 20px; margin: 10px; color: rgb(51, 51, 51); font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif; font-size: 14px;"><pre style="box-sizing: border-box; font-family: Menlo, Monaco, Consolas, 'Courier New', monospace; font-size: 13px; white-space: pre-wrap; padding: 9.5px; margin-top: 0px; margin-bottom: 10px; line-height: 1.42857143; word-break: break-all; word-wrap: break-word; background-color: rgb(245, 245, 245); border: 1px solid rgb(204, 204, 204); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px;">The Marshtomp has seen it all before.
marshTomp is beaten by fjxmlhx!
AmarshtompB
样例输出
The fjxmlhx has seen it all before.
fjxmlhx is beaten by fjxmlhx!
AfjxmlhxB

 
 

#include <string>
#include <iostream>
#include <vector>

using namespace std;


bool isPattern(const string &str1,const string &str2)
{
	string s1(str1);
	string s2(str2);
	if(s1.length()==s2.length())
	{
		int flag=1;
		for(auto &w:s1)
			w=toupper(w);
		for(auto &w:s2)
			w=toupper(w);
		for(int i=0;i<s1.length();i++)
		{
			if(s1[i]!=s2[i])
				flag=0;
		}
		if(flag)
			return true;
		else 
			return false;
	}
	else 
		return false;
}


string strRpl(const string &s1,const string &s2,const string &s3)
{
	string ret;
	string str;
	int n1=s1.length();
	int n2=s2.length();
	int i=0;
	while(i<n1)
	{
		str=s1.substr(i,n2);
		if(isPattern(str,s2))
		{
			ret+=s3;
			i=i+n2;
		}

		else
		{
			ret+=s1[i];
			i++;
		}
	}


	return ret;

}



int main()
{
	string line;
	string ret;
	vector<string> results;
	while(getline(cin,line))
	{
		ret=strRpl(line,"marshtomp","fjxmlhx");
		results.push_back(ret);
	}
	for(auto v:results)
		cout<<v<<endl;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值