The Marshtomp has seen it all before(c/c++字符串输入处理)

本文介绍了一道涉及字符串替换的编程挑战,重点在于处理带有空格的输入,并提供了使用C++实现的具体代码示例,包括如何利用getline和gets进行正确的字符串读取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

fjxmlhx不喜欢网上的 marshtomps 。所以他决定把所有的 “marshtomp”(名字不区分大小写) 改为 “fjxmlhx”;

Input

输入包含多行,每行字符串不超过200的长度,一个单词不会一半出现在上一行,剩下的在下一行。直到文件结束(EOF)

Output

输出 替换之后的字符串。

Sample Input
The Marshtomp has seen it all before.
marshTomp is beaten by fjxmlhx!
AmarshtompB
Sample Output
The fjxmlhx has seen it all before.
fjxmlhx is beaten by fjxmlhx!
AfjxmlhxB

题目看上去很简单,但是有很多处理细节,就是这道题有点难,首先题目输入的字符串是带空格的,而scanf,cin都不会输入空格, gets()可以输入空格,但是和scanf不同,gets()遇到EOF返回NULL,所以循环写成while(gets(s)!=NULL);c++中也给出了输入空格的方法:getline(cin, s)其中s是string类;循环里直接写成while(getline(cin, s))就可以,遇到EOF结束循环;

下面附上代码:


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main(){

   string s;
    char love[]={'f’,'j','x','m','l','h','x', '\0'};
    char hate[]={'m','a','r','s','h','t','o','m','p'};
    while(getline(cin, s)){
        int i, j;
        int len=s.length();
       //printf("len:%d\n", len);
        int flag[205];
        memset(flag, 0, sizeof(flag));
        for(i=0; i<len; i++){
            for(j=0; j<9; j++){
                char a, A;
                a=hate[j];
                A=hate[j]-'a'+'A';
                if(s[i+j]!=a && s[i+j]!=A)
                    break;
            }
             if(j>=9){
                flag[i]=1;
                i+=8;
             }
           //  printf("i:%d j:%d\n", i, j);
            }
        for(i=0; i<len; i++){
            if(flag[i]){
                cout << love;
                i+=8;
            }
            else cout << s[i];
        }
        cout << endl;
    }

    return 0;
}


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <string.h>
#include <stdlib.h>

using namespace std;

int main(){

    char s[205];
    char love[]={'f','j','x','m','l','h','x', '\0'};
    char hate[]={'m','a','r','s','h','t','o','m','p'};
    while(gets(s)!=NULL){
        int i, j;
        int len=strlen(s);
       //printf("len:%d\n", len);
        int flag[205];
        memset(flag, 0, sizeof(flag));
        for(i=0; i<len; i++){
            for(j=0; j<9; j++){
                char a, A;
                a=hate[j];
                A=hate[j]-'a'+'A';
                if(s[i+j]!=a && s[i+j]!=A)
                    break;
            }
             if(j>=9){
                flag[i]=1;
                i+=8;
             }
           //  printf("i:%d j:%d\n", i, j);
            }
        for(i=0; i<len; i++){
            if(flag[i]){
                cout << love;
                i+=8;
            }
            else cout << s[i];
        }
        cout << endl;
    }

    return 0;
}



你必须对该题目采用记忆化搜索!!!!!!!!!!!!我的代码为什么结果输出不对#include<bits/stdc++.h> using namespace std; unordered_map<string,int> mp; int t,n; string s; int dfs(string s) { if(mp.count(s))return mp[s]; if(s.size()==1)return mp[s]=1; string l=s,r=s; int res=0; int a=s.find("01"),b=s.find("10"); if(a==-1&&b==-1)return mp[s]=0; if(a!=-1) { l.replace(a,2,"1"); res+=dfs(l); } if(b!=-1) { r.replace(b,2,"0"); res+=dfs(r); } return mp[s]=res; } int main( ) { cin>>t; while(t–) { cin>>n>>s; cout<<n+dfs(s)<<‘\n’; } return 0; }MC0320 狠狠地对字符串做你想做的事吧 难度: 黄金 时间限制:1秒 占用内存:128 M 收藏 报错 对一个字符串 S S ,有以下两种操作: • 将子串 01 替换为 1 • 将子串 10 替换为 0 总共 t t组数据,每组的 S S都是一个给定长度为 n n的 01 串。 求每组数据中, S S一共有多少个子串,能经过若干次操作,变成长度为1的字符串。 格式 输入格式: 第一行包含一个整数 t ( 1 ≤ t ≤ 1000 ) t(1≤t≤1000) 表示 t t组数据; 每组的第一行包含一个整数 n n表示 S S的长度; 每组的第二行包含一个由 n n个字符 S 1 , S 2 , … , S n S 1 ​ ,S 2 ​ ,…,S n ​ 组成的二进制字符串 S S( 其中 S i 0 S i ​ =0或 S i 1 S i ​ =1)。 输出格式: 针对每个测试用例,输出该字符串满足题意的子串个数。 样例 1 输入: 5 1 1 2 01 3 100 4 1001 5 11111 复制 输出: 1 3 4 8 5 复制 备注 其中: 1 ≤ t ≤ 1000 , 1 ≤ n ≤ 2 × 1 0 5 , ∑ n ≤ 2 ∗ 1 0 5 1≤t≤1000,1≤n≤2×10 5 ,∑ n ​ ≤2∗10 5 ,保证所有测试用例的 n 总和不超过 2 × 1 0 5 2×10 5 。
最新发布
08-22
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值