UVA 10115(字符串)

来源:互联网 发布:看门狗win10优化补丁 编辑:程序博客网 时间:2024/05/21 07:25
题目:Text-processing tools like awk and sed allow you to automatically perform a sequence of editing operationsbased on a script. For this problem we consider the specific case in which we want to performa series of string replacements, within a single line of text, based on a fixed set of rules. Each rulespecifies the string to find, and the string to replace it with, as shown below.Rule Find Replace-by1. ban bab2. baba be3. ana any4. ba b hind the gTo perform the edits for a given line of text, start with the first rule. Replace the first occurrenceof the f ind string within the text by the replace − by string, then try to perform the same replacementagain on the new text. Continue until the f ind string no longer occurs within the text, and then moveon to the next rule. Continue until all the rules have been considered. Note that (1) when searchingfor a f ind string, you always start searching at the beginning of the text, (2) once you have finishedusing a rule (because the f ind string no longer occurs) you never use that rule again, and (3) case issignificant.For example, suppose we start with the linebanana boatand apply these rules. The sequence of transformations is shown below, where occurrences of a f indstring are underlined and replacements are boldfaced. Note that rule 1 was used twice, then rule 2 wasused once, then rule 3 was used zero times, and then rule 4 was used once.Before Afterbanana boat babana boatbabana boat bababa boatbababa boat beba boatbeba boat behind the goatInputThe input contains one or more test cases, followed by a line containing only 0 (zero) that signals theend of the file. Each test case begins with a line containing the number of rules, which will be between1 and 10. Each rule is specified by a pair of lines, where the first line is the f ind string and the secondline is the replace − by string. Following all the rules is a line containing the text to edit.
OutputFor each test case, output a line containing the final edited text.Note: Both f ind and replace − by strings will be at most 80 characters long. f ind strings will containat least one character, but replace − by strings may be empty (indicated in the input file by an emptyline). During the edit process the text may grow as large as 255 characters, but the final output textwill be less than 80 characters long.The first test case in the sample input below corresponds to the example shown above.
Sample Input4banbabbababeanaanyba bhind the gbanana boat1tshtoe or top0Sample Outputbehind the goatshoe or shop
题目大意:
输入自定义的代替规则的数量,然后输入自定义规则。根据规则对输入的字符串进行操作。
方法:
用到了string的函数find和replace.
代码:
#include<stdio.h>#include<string.h>#include<string>#include<stdlib.h>#include<iostream>using namespace std;int main() { char str[5000];char temp[10][100];char now[10][100];string str5;int n = 0;while(scanf("%d",&n) && n) {getchar();memset(temp, 0, sizeof(temp));memset(now, 0, sizeof(now));for(int i = 0; i < n; i++) {gets(temp[i]);gets(now[i]);}gets(str);str5=str;for(int i = 0; i < n;i++) {int f = str5.find(temp[i]);if(f != -1) {str5.replace(f,strlen(temp[i]),now[i]);i -= 1;}}cout << str5 << endl;}return 0;

0 0