【 HihoCoder】1082 The Marshtomp has seen it all before (暴力 或 脑力)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CSDN___CSDN/article/details/88381296

https://vjudge.net/problem/HihoCoder-1082

暴力 

#include <iostream>
#include <cstring>

using namespace std;

char s[205];

int main()
{
	int i;
	while(gets(s))
	{
		i  =0;
		int len = strlen(s);
		while(i<len)
		{
			if((s[i]=='m'||s[i]=='M')&&(s[i+1]=='a'||s[i+1]=='A')&&(s[i+2]=='r'||s[i+2]=='R')&&(s[i+3]=='s'||s[i+3]=='S')&&(s[i+4]=='h'||s[i+4]=='H')&&(s[i+5]=='t'||s[i+5]=='T')&&(s[i+6]=='o'||s[i+6]=='O')&&(s[i+7]=='m'||s[i+7]=='M')&&(s[i+8]=='p'||s[i+8]=='P'))
			{
				cout <<"fjxmlhx";
				i+=9;
			}
			else
			{
				cout <<s[i];
				i++; 
			}				
		}
		cout << endl;
	}
	return 0;
}

string 的 相关操作

注意:

1、getline(cin,s):没有读入字符的时候,就返回false

2、transform(tmp.begin(),tmp.end(),tmp.begin(),::tolower)

将tmp的所有字母都改成小写

需要包含 #include <algorithm>

3、查找函数 size_t pos = s.find(const string& str, size_t pos = 0)

没找到的话,返回s.npos

4、替换函数 

replace(size_type pos(开始替换的位置),size_type cnt(替换的个数), const basic_string str(用来替换的字符串))

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main ()
{
	string s;
	while(getline(cin,s))
	{
		string ans = s;
		transform(ans.begin(),ans.end(),ans.begin(),::tolower);
		size_t pos=0;
		
		while((pos=ans.find("marshtomp"))!=ans.npos)
		{
			s.replace(pos,9,"fjxmlhx");
			ans.replace(pos,9,"fjxmlhx");
		}
		cout << s << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/CSDN___CSDN/article/details/88381296
今日推荐