UVa Automatic Editing

7531 ワード

uvaの問題は本当にいいです。一つ一つの問題に多くの知識ができます。Aが終わってからとても嬉しいです。この問題は二日間使って書きました。一つの問題だけで四つの関数を勉強しました。たくさん成長しました。
Problem E:Automatic Editing
Source file:
autedit.{c cpp java、 pas}
Input file:
autedit.in
Output file:
atoedit.out
Text-processing tools like awk and sed allow you to at t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t t sequene e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e e aaaaaaaaaaaaaaaalow.
 
Rule
Find
Replace-by
1.
バンド
bab
2.
ba
be
3.
あや
any
4.
ba
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 replace ment アゲイン 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 consided.Note that(1)when searching for a findsting、you always start searching at the begining of the text、(2)once you have finished using a rule(because the) find stringのlonger occurs)you never use that rule again,and(3)case is significant.
For example、suppose we start with the line
 
bana boat
 
and appy these rules.The sequence of tranformations is shown below,where occurrences of a find string are underld and replaccemens are boldfaced.Note that rule 1 was used twice,then rule 2 was used onece,then rule 3 was used zertimes,and then rule 4 was used onece.
 
 
Before
After
 
bana boat
babababababababababababana boat
 
babababababababababababana boat
bababababa boat
 
bababababa boat
beba boat
 
beba boat
behind the goat
 
The input contains one or more test cases、followed by a line containing only 0(ゼロ)that signals the end of the file.Each test case Begins with a line the numbers of rules、which will be between ine e e e e e e e e e e e e e e e.ine.ine.seline.seline.seline.ine.ine.ine.ine.ine.ine.ses.ses.ses.ses.ser.ser.ser.ser.ine.ine.ine.ine.in find string and the second line is the replace-by string.Follwing all the rules is a line containing the text to edit.For each test case,output a line containing the final edtext text.
ブック find and replace-by stings will be at most 80 characters long. Find stings will contain at least one character、but replace-bystrigs may be empy(indicated in the input file by an empy line).During the edit process the text may grow as 255 characters,but the final output will be less than 80 characters.long.
The first test case in the sample input below cores ponds to the example shown above.
Example input:
4

ban

bab

baba

be

ana

any

ba b

hind the g

banana boat

1

t

sh

toe or top

0

Example output:
behind the goat

shoe or shop
代わって、再文字の中で置換できるものを見つけたら置換します。
 Note that(1)when searching for a findsting、you always start searching at the begining of the text、(2)once you have finished using a rule(because the) find stringのlonger occurs)you never use that rule again,and(3)case is significant.
For example、suppose we start with the line。一つ見つけたらまた始めます。文法が終わったら使えなくなります。という意味です
#include<stdio.h>

#include<string.h>

struct Node

{

    int len1,len2;

    char nam1[100],nam2[100];

}word[100];



int main()

{

    int n,i,j;

    char *pt,*px;

    char str[500],rem[500],ss[500];

    int cas,flag;

    while(scanf("%d",&n) && n)

    {

        getchar();

        for (i=0;i<n;i++)

        {

            gets(word[i].nam1);

            word[i].len1=strlen(word[i].nam1);

            gets(word[i].nam2);

            word[i].len2=strlen(word[i].nam2);

        }

        gets(str);

        strcpy(rem,str);

        for (i=0;i<n;i++)//    

        {

            flag=0;

            pt=strstr(rem,word[i].nam1);

            if(pt!=NULL) flag=1;

            else continue;

            cas=0;

            for (px=rem;px!=pt;px++)//      

            {

                ss[cas++]=*px;

            }

            ss[cas]='\0';

            pt+=word[i].len1;//        

            strcat(ss,word[i].nam2);//    

            strcat(ss,pt);//      

            strcpy(rem,ss);

            if(flag) i-=1;

        }

        printf("%s
",rem); } return 0; }