NC214836. 字符串替换(新生题)
描述
输入描述
输入一段字符串
输出描述
将字符替换后的结果输出
示例1
输入:
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
C++(clang++ 11.0.1) 解法, 执行用时: 2ms, 内存消耗: 384K, 提交时间: 2023-05-07 14:39:19
#include<bits/stdc++.h> using namespace std; void change(char *a){ string b=a; for(int i=0;i<b.size();i++){ if((a[i]=='M'||a[i]=='m')&&(a[i+1]=='a'||a[i+1]=='A')&&(a[i+2]=='r'||a[i+2]=='R')){ b.replace(i,9,"fjxmlhx"); } } cout<<b<<endl; } int main(){ char a[100]; while(cin.getline(a,100)){ change(a); } return 0; }
Java(javac 1.8) 解法, 执行用时: 27ms, 内存消耗: 12276K, 提交时间: 2021-03-20 10:12:47
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner= new Scanner(System.in); while (scanner.hasNextLine()) { String string=scanner.nextLine(); System.out.println(string.replaceAll( "(?i)"+"Marshtomp", "fjxmlhx")); } } }
pypy3(pypy3.6.1) 解法, 执行用时: 108ms, 内存消耗: 27632K, 提交时间: 2020-12-13 14:28:01
import re reg = re.compile(re.escape('marshtomp'), re.IGNORECASE) while True: try: a = input() a = re.sub('marshtomp','fjxmlhx',a,flags = re.IGNORECASE) print(a) except: break