10115-automatic Editing
Time limit:3.000 seconds
Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=96&page=show_problem &problem=1056
Text-processing tools like awk and sed allow your to automatically perform a sequence of editing operations based on a SCRI Pt. For this problem we consider the specific case in which we want to perform a series of string replacements, within a Singl E line of text, based on a fixed set of rules. Each rule is 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 "the". Replace the occurrence of the find string within the text by the replace-by string, then try to perform the same rep Lacement again on the new text. Continue until the find string no longer occurs within the "text" and "then move on" to "next rule. Continue until all the rules have been considered. Note this (1) when searching for a find string, your always start searching at the beginning of the text, (2) once to you have Finished using a rule (because TheFind string no longer occurs) in never use this rule again, and (3) the case is Significan T.
For example, suppose we start with the
Banana Boat
and apply these rules. The sequence of transformations is shown below where occurrences of a find string are underlined and replacements are dfaced. Note This rule 1 is used twice, then rule 2 is used once, then rule 3 is used zero times, and then rule 4 is used once .
Before after
Banana Boat Babana Boat
Babana Boat Bababa Boat
Bababa Boat Beba Boat
Beba Boat Behind the Goat
The input contains one or more test cases and 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 would be between 1 and 10. The specified by a pair of lines, where the ' the ' is the ' Find string ' and ' second ' is ' the ' replace-by s Tring. Following all the rules are a line containing the text to edit. For each test case, output a line containing the final edited text.
Both Find and replace-by strings is at most characters long. Find strings'll contain at least one character, but replace-by strings May is empty (indicated in the ' input file by ' E Mpty line). During the edit process the text may grow as large as 255 characters, but the final output text would be less than Cters long.
The ' the ' the ' the ' sample input below corresponds 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 the combined use of strcpy () and strcat () techniques.
Complete code:
/*0.015s*/
#include <cstdio>
#include <cstring>
char find[50][100], rep[50][100], str[300], TMP[1000];
int main (void)
{
int t, I;
Char *beg, *end;
while (scanf ("%d\n", &t), T)
{for
(i = 0; i < T; i++)
{
gets (find[i));
Gets (Rep[i]);
Gets (str);
for (i = 0; i < T; i++)
{
while (beg = Strstr (str, find[i))///assignment and Judge Non-null
{End
= Beg + strlen (find[i) );
strcpy (TMP, rep[i]);
STRCAT (TMP, end);
strcpy (Beg, TMP);
}
Puts (str);
return 0;
}
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/