The question of the string has come to an end, but balabala does not know what to say in English.
The algorithm is also very simple. It can be done with a few library functions. I was worried about what I said.Replace-If it is null, special processing is required. Later, it is found that the processing can also be performed by a in general cases.
The first Re is because Char T [] is small.
By the way, I used the strstr () function for the first time. If I am not familiar with this function, call the portal!
Strstr () for string operations in C Language ()
Search and replace the strings to be searched and replaced, and text. To put it bluntly, it is the search and replacement function in word.
However, it should be noted that a new find substring may appear after "find" is replaced, that is, "find" may be replaced multiple times.
Problem E: automatic editing
| Source file: |
Autoedit.{C,CPP,Java,PAS} |
| Input file: |
Autoedit. In |
| Output file: |
Autoedit. Out |
Text-processing tools likeAwkAndSedAllow 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- |
| 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 ofFindString within the text byReplace-String, then try to perform the same replacementAgainOn the new text. continue untilFindString 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 forFindString, you always start searching at the beginning of the text, (2) once you have finished using a rule (becauseFindString 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 ofFindString 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 |
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 isFindString and the second line isReplace-String. following all the rules is a line containing the text to edit. For each test case, output a line containing the final edited text.
BothFindAndReplace-Strings will be at most 80 characters long.FindStrings will contain at least one character,Replace-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.
The first test case in the sample input below corresponds to the example shown above.
Example input:
4banbabbababeanaanyba bhind the gbanana boat1tshtoe or top0
Example output:
behind the goatshoe or shop
AC code:
1 // # define local 2 # include <iostream> 3 # include <cstdio> 4 # include <cstring> 5 using namespace STD; 6 7 char find [11] [85], replace [11] [85]; 8 char text [260]; 9 10 int main (void) 11 {12 # ifdef local13 freopen ("10115in.txt", "r ", stdin); 14 # endif15 int N; 16 while (scanf ("% d", & n) = 1 & N) 17 {18 getchar (); 19 int I; 20 For (I = 0; I <n; ++ I) 21 {22 gets (find [I]); 23 gets (Replace [I]); 24} 25 memset (text, 0, sizeof (text); 26 gets (text); 27 28 char * s, t [260]; // 85 at the beginning, then re 29 for (I = 0; I <n; ++ I) 30 {31 While (S = strstr (text, find [I]) // a find [I] may be replaced multiple times 32 {33 strcpy (t, s + strlen (find [I]); // store the strings to be connected in T for 34 * s = '\ 0'; 35 // If (strlen (Replace [I]) // strlen (Replace [I]) = 0 can be processed as normal. 36 // {37 strcat (text, replace [I]); // connect the string 38 strcat (text, T) to be replaced; // connect the last small tail 39 //} 40 // else41 // strcat (text, t ); 42} 43} 44 45 cout <text <Endl; 46} 47 return 0; 48}Code Jun