UVA 10115 子符串替换

Text-processing tools like awk and sed allow you to automatically perform a sequence of editing operations based on a script. For this problem we consider the specific case in which we want to perform a series of string replacements, within a single line of text, based on a fixed set of rules. Each rule specifies the string to find, and the string to replace it with, as shown below.

Rule Find Replace-by
1. ban bab
2. baba be
3. ana any
4. ba b hind the g

To perform the edits for a given line of text, start with the first rule. Replace the first occurrence of the find string within the text by thereplace-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. Continue until all the rules have been considered. Note that (1) when searching for a findstring, you always start searching at the beginning of the text, (2) once you have finished using a rule (because the find string no longer occurs) you never use that rule again, and (3) case is significant.

For example, suppose we start with the line

banana boat

and apply these rules. The sequence of transformations is shown below, where occurrences of a find string are underlined and replacements are boldfaced. Note that rule 1 was used twice, then rule 2 was used once, then rule 3 was used zero times, and then rule 4 was used once.

  Before After
  banana boat babana boat
  babana boat bababa boat
  bababa boat beba boat
  beba boat behind the goat

Input

The input contains one or more test cases, followed by a line containing only 0 (zero) that signals the end of the file. Each test case begins with a line containing the number of rules, which will be between 1 and 10. Each rule is specified by a pair of lines, where the first line is the find string and the second line is the replace-by string. Following all the rules is a line containing the text to edit.

Output

For each test case, output a line containing the final edited text.

Both find and replace-by strings will be at most 80 characters long. Find strings will contain at least one character, but replace-by strings may be empty (indicated in the input file by an empty line). During the edit process the text may grow as large as 255 characters, but the final output text will be less than 80 characters long.

Sample Input

4
ban
bab
baba
be
ana
any
ba b
hind the g
banana boat
1
t
sh
toe or top
0

Sample Output

behind the goat
shoe or shop

Hint

when u read a line of string after an int, u can use following codes, like

scanf("%d",&t);getchar();gets(str);

or u may read some unexpected words

题意:给定多组的字符串和用来替换他的字符串的规则,在一段话中按照替换规则替换,如果找到就进行替换,需要注意的是如果前面的规则已经运用完毕,后面就不能拿过来再用;

解题思路:用stl暴力一下;

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<algorithm>
 6 #include<string>
 7 using namespace std;
 8 int main()
 9 {
10     ios::sync_with_stdio(false);
11  // freopen("in.txt","r",stdin);
12     int n;
13     string str1[11],str2[11],str3;
14     while(1){
15           cin>>n;
16             if(n==0) break;
17         string nn;
18         getline(cin,nn);
19         for(int i=0;i<n;i++)
20         {
21             getline(cin,str1[i]);
22             getline(cin,str2[i]);
23         }
24         getline(cin,str3);
25         bool flag;int num=0;
26         while(1){
27             flag=1;
28             for(int i=num;i<n;i++)
29             {
30                 if(i>num) num=i;
31                 int first=str3.find(str1[i],0);
32                 if(first!=string::npos)
33                 {
34                     flag=0;
35                     str3.replace(first, str1[i].size(),str2[i]);
36                     break;
37                 }
38             }
39             if(flag) break;
40         }
41         cout<<str3<<endl;
42     }
43     return 0;
44 }
时间: 2024-10-07 11:31:50

UVA 10115 子符串替换的相关文章

ios-字符串替换-正则表达式-例子

需求:在html中查找并替换相应的html标签 代码实现 - (NSString *)replaceImageHtml:(NSString *)oldHtml { NSString *regex = @"(<img.*?/>)"; NSRange r; NSMutableString *newHtml = [NSMutableString stringWithString:oldHtml]; BOOL flag = false; while (flag == false)

【代码笔记】iOS-字符串替换回车和换行

一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //替换回车符和空格 NSString *oldStr=@" 1 2 4 "; NSLog(@"----oldStr---%@",oldStr); //去掉首尾空格 NSString *newStr=[oldStr strin

NYoj-字符串替换

字符串替换 时间限制:3000 ms  |  内存限制:65535 KB 难度:2 描述 编写一个程序实现将字符串中的所有"you"替换成"we" 输入 输入包含多行数据 每行数据是一个字符串,长度不超过1000 数据以EOF结束 输出 对于输入的每一行,输出替换后的字符串 样例输入 you are what you do 样例输出 we are what we do 错误思路: #include<cstdio> #include<cstring&

31-语言入门-31-字符串替换

题目地址:?http://acm.nyist.net/JudgeOnline/problem.php?pid=113? ? 描述编写一个程序实现将字符串中的所有"you"替换成"we" 输入输入包含多行数据 每行数据是一个字符串,长度不超过1000 数据以EOF结束输出对于输入的每一行,输出替换后的字符串样例输入you are what you do样例输出 we are what we do ? ? 代码: #include <stdio.h> //处

string 子符串的查找、插入、删除操作

6 string s; 7 s = "124567"; 8 string::iterator it; 9 it = s.begin();//让s指向第一个元素 10 cout << s; 11 system("pause"); 12 return 0; 在未进行插入之前的运行结果: 进行插入操作后运行结果,在字符串第二个元素后面进行插入元素 ‘3’ 操作: 1 string::iterator it; 2 it = s.begin(); 3 s.inse

UVa 10115 - Automatic Editing

题目:给你一些字符串的替换关系,以及一个句子.按顺序替换,输出最后结果. 分析:字符串.按照替换顺序依次替换(这个替换用过之后,就不再使用),每个替换可能出现多次. 这里注意,如果当前串中有多个可被当前单词替换的位置,只替换最前面的那个, 下次用本次生成的串替换,而不是整体一次性替换. 说明:注意数据清空. #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio>

JavaScript--字符串常用方法总结

JavaScript--字符串常用方法总结 举例模板: var str = "what are you " var str1 = "sss" var txt = str.方法(参数1,参数2...) console.log(txt) 1.charAt(索引):根据索引,返回字符串中指定的字符串 var txt = str.charAt(5) 2.charCodeAt(索引):根据索引,返回字符串中指定字符串的ASCII编码 var txt = str.charCod

java学习笔记05--字符串 .

java学习笔记05--字符串 . 一.String类 由字符所组成的一串文字符号被称之为字符串.在java中字符串不仅仅是字符数组,而且是String类的一个实例,可以使用String类来构建. 字符串的每个字符是使用Unicode字符来构建. Sring对象上的几个方法: length()       取得字符串的长度 equals()      判断源字符串中的字符是否等于指定字符串中的字符  toLowerCase()      转换字符串中的英文字符为小写 toUpperCase() 

【华为OJ】【023-字符串加解密】

[华为OJ][算法总篇章] [华为OJ][023-字符串加解密] [工程下载] 题目描述 1.对输入的字符串进行加解密,并输出. 2加密方法为: 当内容是英文字母时则用该英文字母的后一个字母替换,同时字母变换大小写,如字母a时则替换为B:字母Z时则替换为a: 当内容是数字时则把该数字加1,如0替换1,1替换2,9替换0: 其他字符不做变化. 3.解密方法为加密的逆过程. 接口描述:实现接口,每个接口实现1个基本操作: void encrypt (char aucPassword[], char