hihocoder 1082 然而沼跃鱼早就看穿了一切 (替换指定的串 )

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

时间限制: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

算法:这是一道字符串处理问题,就是在原字符串遇到一串特殊字符串,我不输出它
我用其他的一串来替代输出!
先将原字符串的小写化存在另一个串里,利用该串和那段特殊串进行一个查找
的过程,就是找出这个特殊串在处理后的原串里的所有位置找出来并存储。
再对未处理的原串进行输出,如果遇到那些记录的特殊位置就输出特殊串的替代串,
就这样ok!


代码:
#include <string.h>
#include <stdio.h>
#include <ctype.h>

int a[200];
int e;

void Find_pos(char *s, char *t)
{
    int i, j;
    int len, ll;
    len=strlen(s)-strlen(t);
    ll=strlen(t);

    e=0;
    memset(a, -1, sizeof(-1));
    for(i=0; i<=len; i++)
    {
        if(s[i]==t[0])
        {
            for(j=1; j<ll; j++)
            {
                if(s[i+j]!=t[j] )
                {
                    break;
                }
            }
            if(j==ll)
            {
                a[e++]=i;
            }
            j=0;
        }
    }
}


int main()
{
    char s[300];
    char ss[300];
    char *t="marshtomp";
    int i, j;
    int len;

    while(gets(s)!=NULL )
    {
        len=strlen(s);

        for(i=0; i<len; i++)
        {
            ss[i]=tolower(s[i]);
        }
        Find_pos(ss, t);
        int k=0;
        for(i=0; i<len; )
        {
            if(i==a[k])
            {
                printf("fjxmlhx");
                i+=9;
                k++;
            }
            else
            {
                printf("%c", s[i]);
                i++;
            }
        }
        printf("
");
    }
    return 0;
}

原文地址:https://www.cnblogs.com/yspworld/p/4176079.html