Problem D: Automatic Editing
DescriptionText-processing tools like awk and sed allow 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-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 occurrence of the find string within the text by the replace-by string, then try to perform the same
replacement again 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 considered. Note that (1) when searching for a find string, you always start searching
at the beginning of the text, (2) once you have finished using a rule (because the find string no longer occurs) you never use that rule again, and (3) case is significant.For example, suppose we start with the linebanana boatand apply these rules. The sequence of transformations is shown below, where occurrences of a find string 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 Afterbanana boat babana boatbabana boat bababa boatbababa boat beba boatbeba boat behind the goatThe 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 is the find string and the second line is the replace-by 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.Both find and replace-by strings will be at most 80 characters long. Find strings will contain at least one character, but replace-by 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.InputThe 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 is the find string and the second line is the replace-by 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. Both find and replace-by strings will be at most 80 characters long. Find strings will contain at least one character, but replace-by 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.
output
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解题思路:
字符串替换问题,不难
1、分别把需要置换的字符串 和 置换后的字符串存起来
2、利用循环进行置换,置换一次之后再回溯一次,直到没有可以置换的字符串后,再进行下个字符串的置换
#include <stdio.h>
#include <string.h>
#define M 20
char c_b1[M][M]; //存须置换的字符串
char c_b2[M][M]; //存置换成的字符串
int main()
{
int m;
int flag;
char *pt,*p;
char c[100],zz[100],ss[100]; //c为输入的字符数组,zz为操作中转,ss为得到的新的字符数组
while (scanf("%d",&m)!=EOF)
{
if (m==0)
break; //0时结束
getchar();
for (int i=0; i<m; i++)
{
gets(c_b1[i]);
gets(c_b2[i]);
}
gets(c);
strcpy(zz,c);
for (int i=0; i<m; i++) //m种置换
{
flag = 0;
pt=strstr(zz,c_b1[i]);
if (pt!=NULL)
flag=1;
else
continue;
int j=0;
for (p=zz; p!=pt; p++) //存前一半部分
{
ss[j++] = *p;
}
ss[j]='';
pt += strlen(c_b1[i]); //后移,抛开不要的
strcat(ss,c_b2[i]); //把置换的放进来
strcat(ss,pt); //接入剩余的
strcpy(zz,ss); //放到zz中
if (flag) i--; //重新再来
}
printf("%sn",zz);
}
return 0;
}
本文探讨了使用脚本命令自动执行字符串替换操作的过程,详细介绍了如何基于预定义规则序列化地修改文本内容,包括从查找字符串到替换字符串的整个流程,以及在编辑过程中可能出现的情况如重复使用规则和忽略大小写敏感性。
1万+

被折叠的 条评论
为什么被折叠?



