• AC日记——单词替换 1.7 21


    21:单词替换

    总时间限制: 
    1000ms
     
    内存限制: 
    65536kB
    描述

    输入一个字符串,以回车结束(字符串长度<=100)。该字符串由若干个单词组成,单词之间用一个空格隔开,所有单词区分大小写。现需要将其中的某个单词替换成另一个单词,并输出替换之后的字符串。

    输入
    输入包括3行,
    第1行是包含多个单词的字符串 s;
    第2行是待替换的单词a(长度 <= 100);
    第3行是a将被替换的单词b(长度 <= 100).

    s, a, b 最前面和最后面都没有空格.
    输出
    输出只有 1 行,将s中所有单词a替换成b之后的字符串。
    样例输入
    You want someone to help you
    You
    I
    
    样例输出
    I want someone to help you
    
    来源
    医学部计算概论2006期末考试题

    思路:

      大模拟;

    来,上代码:

    #include<cstdio>
    #include<string>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    
    using namespace std;
    
    int len_1,len_2,len_3;
    
    char word[10001],wait_1[101],wait_2[101];
    
    int main()
    {
        gets(word);
        gets(wait_1);
        gets(wait_2);
        len_1=strlen(word),len_2=strlen(wait_1),len_3=strlen(wait_2);
        for(int i=0;i<len_1-len_2+1;i++)
        {
            if(word[i]==wait_1[0])
            {
                bool if_ok=true;
                for(int j=i;j<=len_2+i-1;j++)
                {
                    if(wait_1[j-i]==word[j]) continue;
                    if_ok=false;
                    break;
                }
                if(if_ok)
                {
                    if((i-1==-1||word[i-1]==' ')&&(i+len_2-1==len_1-1||word[i+len_2]==' '))
                    for(int j=i;j<=len_2+i-1;j++) word[j]='^';
                }
            }
        }
        for(int i=0;i<len_1;i++)
        {
            if(word[i]=='^')
            {
                if(i-1==-1||word[i-1]!='^') cout<<wait_2;
                continue;
            }
            putchar(word[i]);
        }
        return 0;
    }
  • 相关阅读:
    Codeforces 653C Bear and Up-Down【暴力】
    Codeforces 653B Bear and Compressing【DFS】
    Codeforces 653B Bear and Compressing【DFS】
    Codeforces 653A Bear and Three Balls【水题】
    Codeforces 645D Robot Rapping Results Report【拓扑排序+二分】
    Codeforces 645C Enduring Exodus【二分】
    软件工程作业01
    《构建之法》阅读笔记5
    登录界面代码
    《构建之法》读书笔记4
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6105290.html
Copyright © 2020-2023  润新知