时间限制:1000ms
单点时限:1000ms
内存限制:256MB
fjxmlhx每天都在被沼跃鱼刷屏,因此他急切的找到了你希望你写一个程序屏蔽所有句子中的沼跃鱼(“marshtomp”,不区分大小写)。为了使句子不缺少成分,统一换成 “fjxmlhx” 。
输入包括多行。
每行是一个字符串,长度不超过200。
一行的末尾与下一行的开头没有关系。
输出包含多行,为输入按照描述中变换的结果。
样例输入
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
题目链接:https://hihocoder.com/problemset/problem/1082
分析:自己看吧!
下面给出AC代码:
1 #include<iostream>
2 #include <string.h>
3 using namespace std;
4
5 typedef long long ll;
6
7 int main(int argc , const char * argv[])
8 {
9 char s[200];
10 while (cin.getline(s,200))
11 {
12 char f[] = "marshtomp";
13 char t[] = "fjxmlhx";
14 int sl = strlen(s);
15 int fl= strlen(f);
16 int tl = strlen(t);
17 for (int i =0; i<strlen(s); i++)
18 {
19 int j = 0;
20 int k =i;
21 while (s[k] == f[j] || s[k] == (f[j]-32))
22 {
23 k++;
24 j++;
25 if (j == fl)
26 {
27 for (int q = i; (q-i) < tl; q++)
28 {
29 s[q] = t[q-i];
30 }
31 for (int q = i+tl; q<strlen(s); q++)
32 {
33 if ( s[q+fl-tl] != '\0')
34 {
35 s[q] = s [q+fl-tl];
36 }
37 else
38 {
39 s[q] = '\0';
40 }
41 }
42 }
43 }
44 }
45 cout<<s<<endl;
46 }
47
48 }
以上是官方代码,今天上课讲了一个cin.getline(),于是把我之前一直没有过的代码稍微改动了一下输入格式,要用while循环连续输入,否则会WA!
下面给出我的简洁版的AC代码:
1 #include <bits/stdc++.h>
2 using namespace std;
3 int main()
4 {
5 char s[210];
6 while(cin.getline(s,210))//连续输入,cin.getline()的用法就是这样用,具体参照我的博客
7 {
8 for(int i=0;s[i]!='\0';i++)
9 {
10 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'))
11 {
12 s[i]='f';
13 s[i+1]='j';
14 s[i+2]='x';
15 s[i+3]='m';
16 s[i+4]='l';
17 s[i+5]='h';
18 s[i+6]='x';//这个if循环就是无视大小写,直接换
19 for(int j=i+7;s[j]!='\0';j++)
20 s[j]=s[j+2];//换完以后再挪位置
21 }
22 }
23 cout<<s<<endl;
24 }
25 return 0;
26 }