uva10115

来源:互联网 发布:淘宝买家申请售后换货 编辑:程序博客网 时间:2024/06/05 15:18

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1056

10115 - Automatic Editing

Time limit: 3.000 seconds

Problem E: Automatic Editing

Source file:autoedit.{c, cpp,java,pas}Input file:autoedit.inOutput file:autoedit.out

Text-processing tools like awk and sed allow youto automatically perform a sequence of editing operations based ona script. For this problem we consider the specific case in whichwe want to perform a series of string replacements, within a singleline of text, based on a fixed set of rules. Each rule specifiesthe string to find, and the string to replace it with, as shownbelow.

RuleFindReplace-by1.banbab2.bababe3.anaany4.ba bhind the g

To perform the edits for a given line of text, start with thefirst rule. Replace the first occurrence of the find stringwithin the text by the replace-by string, then try toperform the same replacement again on the new text.Continue until the find string no longer occurs within thetext, and then move on to the next rule. Continue until all therules have been considered. Note that (1) when searching for afind string, you always start searching at the beginning ofthe text, (2) once you have finished using a rule (because thefind 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 shownbelow, where occurrences of a find string are underlined andreplacements are boldfaced. Note that rule 1 was used twice, thenrule 2 was used once, then rule 3 was used zero times, and thenrule 4 was used once.

BeforeAfterbanana boatbabana boatbabana boatbababa boatbababa boatbeba boatbeba boatbehind the goat

The input contains one or more test cases, followed by a linecontaining only 0 (zero) that signals the end of the file. Eachtest case begins with a line containing the number of rules, whichwill be between 1 and 10. Each rule is specified by a pair oflines, where the first line is the find string and thesecond line is the replace-bystring. Following all the rulesis a line containing the text to edit. For each test case, output aline containing the final edited text.

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

The first test case in the sample input below corresponds to theexample shown above.

Example input:

4banbabbababeanaanyba bhind the gbanana boat1tshtoe or top0

Example output:

behind the goatshoe or shop
题意:将列表第一列的字符串不断换成第二列的字符串,直到将所给的整个句子中所有出现的第一列字符串全部换掉为止,然后再进行下一个字符串的替换。
strstr(str1,str2).找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)。  
代码:
#include<stdio.h>
#include<string.h>
int main()
{
 int T,i,l;
 char st1[50][80],st2[50][80],st[500]={'\0'},_st[500];
 while(scanf("%d",&T)!=EOF)//输入测试数据有多少组
 {
  if(T==0)//T==0表示整个结束
   return 0;
  getchar();//吸收换行
  for(i=0;i<T;i++)
  {
   gets(st1[i]);//输入字符串1
   gets(st2[i]);//输入字符串2
  }
  gets(st);//输入字符串
  for(i=0;i<T;i++)
  {
   if(strstr(st,st1[i])!=NULL)//
   {
    char *p=strstr(st,st1[i]);//找出str2字符串在str1字符串中第一次出现的位置(不包括str2的串结束符)
    l=strlen(st1[i]);//求出st1[i]的长度
    strcpy(_st,p+l);//复制从p+l指针后面的字符到_st中保存
    *p='\0';//
    //puts(_st);
    strcat(st,st2[i]);//把str2[]链接到st中去将要替换的部分进行替换(将要替换后边的字符串也覆盖掉了)
    strcat(st,_st);//把_st链接到st中。将暂存的字符串补上
    i--;//不理解**************
    //break;
   }
  }
  puts(st);
 }
 return 0;
} 
原创粉丝点击