POJ 1572 Automatic Editing 字符串替换,replace就够了
题意不难理解,但是一开始还是没有看清楚题目。Replace the first occurrence of the find string within the text by the replace-by string, then try to perform the same replacement again on the new text. Continue until the find string no longer occurs within the text, and then move on to the next rule.这个地方我贡献了几次TL。它的意思是替换完第n个字符串后,继续判断是否还能被这个字符串替换,否则移至下一个。
#include <iostream>
#include <cstdio>
#include <string>
using namespace std; int main()
{
//freopen("in.txt","r",stdin);
while(1)
{
int n;
scanf("%d",&n);
if(n==0)
break;
string str1[12];
string str2[12];
getchar();
for(int i=0; i<n; i++)
{
getline(cin,str1[i]);
getline(cin,str2[i]);
}
string str3;
getline(cin,str3);
for(int i=0; i<n; i++)
while(1)
{
int index=str3.find(str1[i]); //查找要替换的字符串位置,没有返回string::npos
if(index==string::npos)
break;
str3=str3.replace(index,str1[i].length(),str2[i]); //replace函数直接替换,或者用erase和insert也行
//str3.erase(index,str1[i].length());
//str3.insert(index,str2[i]);
}
cout<<str3<<endl;
}
return 0;
}
POJ 1572 Automatic Editing 字符串替换,replace就够了的更多相关文章
- 字符串替换replace方法
字符串替换replace方法: http://www.w3school.com.cn/jsref/jsref_replace.asp http://www.cnblogs.com/skywang/ar ...
- Python3字符串替换replace(),translate(),re.sub()
Python3的字符串替换,这里总结了三个函数,replace()和translate()和re.sub() replace() replace() 方法把字符串中的 old(旧字符串) 替换成 ne ...
- C#不区分大小写的字符串替换(Replace)函数
在.NET中,不调用C++/CLI,进行字符串替换有好几种方法: 1.最常用的,就是String实例.Replace(),但这个不能忽略大小写. 2.System.Text.Regex(Regular ...
- oracle学习笔记:字符串替换 replace、regexp_replace、translate函数
1.replace 函数 语法:replace(char, search_string, replacement_string) --针对字符串替换 功能: 将char中的字符串替换. 当re ...
- C# 字符串替换Replace
C# 中的石strA.Replace(strB,strC)函数可以实现将strA中的strB替换为strC. 但是容易出错的地方是,这并不是就直接替换好了,此函数的返回值才是替换好的字符串,所以还要要 ...
- JQuery字符串替换replace方法
在日常的js开发中,常常会用到JQuery, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replac ...
- C#自定义字符串替换Replace方法
前一阵遇到一个如标题的算法题,是将原有字符串的某些片段替换成指定的新字符串片段,例如将源字符串:abcdeabcdfbcdefg中的cde替换成12345,得到结果字符串:ab12345abcdfb1 ...
- JavaScript字符串替换replace方法
在日常的js开发中, 当要把字符串中的内容替换时,如果使用类似C#的string.replace方法,如下 var str='aabbccaa'; str=str.replace('aa','dd') ...
- Java之字符串替换replace()
replace(char oldChar, char newChar)返回一个新的字符串,它是通过用 newChar 替换此字符串中出现的所有 oldChar 而生成的 import java.uti ...
随机推荐
- wps中新罗马字体如何设置Times New Roman
word wps中新罗马字体如何设置Times New Roman ### WPS字体自带 Times New Roman ###
- ssh判断免密登陆
ssh判断免密登陆 [root@jenkins ~]# vi /opt/release_code.sh #!/bin/bash . /etc/init.d/functions #echo $WORKS ...
- nginx location标签的匹配规则
location的匹配 匹配符 匹配规则 优先级 = 精确匹配 1 ^~ 以某个字符串开头 2 ~ 区分大小写的正则匹配 3 ~* 不区分大小写的正则匹配 4 !~ 区分大小写不匹配的正则 5 !~* ...
- 046.Python协程
协程 1 生成器 初始化生成器函数 返回生成器对象,简称生成器 def gen(): for i in range(10): #yield 返回便能够保留状态 yield i mygen = gen( ...
- DOCKER学习_012:Dockerfile配置指令详解
1 Dockerfile结构 基础镜像信息 镜像操作指令 容器启动时执行指令 2 FROM 指定基础镜像,用于继承其他镜像使用的 FROM ubuntu:14.06 FROM centos FROM ...
- 在安装python 第三方库时遇到【WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, st】问题
在命令执行窗输入: pip install Pyinstaller -i http://pypi.douban.com/simple --trusted-host pypi.douban.com (其 ...
- 安装tomcat8 env
目录 1 download package 2 install tomcat 3 Pro config 4. docker image 1 download package wget https:// ...
- SpringBoot实现整合mybatis-generator-maven-plugin 1.4.0
创建 Maven 工程 网上有很多教程且 Idea 可以直接创建 这里就不进行 pom.xml 引入依赖和插件 pom中generalto-maven-plugs中必须指定mysql驱动,并且明确版本 ...
- 微信小程序setdata修改数组或对象
1.this.setdata修改数组的固定一项的值 changeItemInArr: function() { this.setData({ 'arr[0].text':'changed data' ...
- 在Docker中安装MongoDB
在Docker中安装MongoDB docker run -p 27017:27017 -v /data/mongodb:/data/db --name mongodb -d mongo --auth ...