您的位置:首页 > 其它

#1082 : 然而沼跃鱼早就看穿了一切

2015-04-16 19:34 267 查看
时间限制: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


分析:暴力搜就不说了。说一种用状态机的方法,学过编译原理的童鞋知道确定性状态机,这个是一个检测marshtomp字符串的题目。看程序吧,非常简单,状态从0到8,每检测到一个这个单词的字母就状态转换,
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;

int main()
{
char str[201];
const char str_AZ[]="MARSHTOMP";
const char str_az[]="marshtomp";
while(cin.getline(str,200))
{
int i=-1;
int state=0;
char *s;
while(str[++i]!='\0')
{
if(state==0)
{
if(str[i]=='M'||str[i]=='m')
{
s = &str[i];
state++;
}
}
else if(state==8)
{
if(str[i]==str_AZ[state] || str[i]==str_az[state])
{
strcpy(s,"fjxmlhx");
strcpy(s+7,str+i+1);
i-=2;
}
state=0;
}
else
{
if(str[i]==str_AZ[state] || str[i]==str_az[state])
state++;
else
state=0;
}
}
printf("%s\n",str);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: