/*
Rule Find Replace-by
1. ban bab
2. baba be
3. ana any
4. ba b hind the g
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 line
banana boat
and 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 After
(ban)ana boat babana boat
ba(ban)a boat bababa boat
(baba)ba boat beba boat
be(ba b)oat behind the goat
NOTE:
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.
规则比较多,替换字符串可以为空,最后输出的字符串长度总是不大于80个
*/
#include <stdio.h>
#include <string.h>
#define MAXSIZE 256
char szFindStr[10][MAXSIZE]; //Find字符数组
char szReplaceStr[10][MAXSIZE]; //Replace字符数组
int szFind(char szFinalText[MAXSIZE],char szFindStr[MAXSIZE])
{
int i = 0,j = 0;
int iLeni = strlen(szFinalText),iLenj = strlen(szFindStr);
while(i < iLeni && j < iLenj)
{
if(szFinalText[i] == szFindStr[j])
{
i = i+1;
j = j+1;
}
else
{
i = i-j+1;
j = 0;
}
}
if(j >= iLenj)return i-j;
return -1;
}
//字符串编辑函数
void szEdit(char szFinalText[MAXSIZE],int iCase)
{
/*when searching for a find string,
you always start searching at the beginning of the text,
这句话的意思就是当替换一个字符串后,下一次查找替换都要从头开始*/
int i;
//临时数组
char szTemp[MAXSIZE];
for(i = 0; i < iCase; i++)
{
int iPos=-1;
//在while里,当查找到某次要替换的字符串时iPos为>-1 否则为-1
do
{
int j,k,m,iFLength/*查找长度*/ = strlen(szFindStr[i]),
iRLength/*替换长度*/ = strlen(szReplaceStr[i]);
iPos = szFind(szFinalText,szFindStr[i]);
if(-1 != iPos)
{
int iFinalLen = strlen(szFinalText);
//第一步先把0~iPos复制到szTemp
for(j = 0; j < iPos; j++)szTemp[j] = szFinalText[j];
//第二步再把replace String替换到szTemp中
for(m = j,k = 0; k < iRLength; m++,k++)szTemp[m] = szReplaceStr[i][k];
//第三步再把szFinalText中j+srlen(szFindStr[i]) ~ end复制到szTemp中
for(int t = j+iFLength; t < iFinalLen; t++,m++)szTemp[m] = szFinalText[t];
szTemp[m] = '\0';
//第四步在把szTemp复制到szFinalText中
int len = strlen(szTemp);
for(j = 0; j < len; j++)szFinalText[j] = szTemp[j];
szFinalText[len] = '\0';
}
}while(iPos != -1);
}
}
int main()
{
int iCase;
while(scanf("%d",&iCase))
{
//程序跳出点
if(0 == iCase)break;
//gets会接收'\0',所以要过滤一个回车符
getchar();
//****************
int i = 0;
char szFinalText[MAXSIZE];
//****************
//初始化 Find Replace二维字符数组
for(i = 0; i < 10; i++)
{
memset(szFindStr[i],0,MAXSIZE);
memset(szReplaceStr[i],0,MAXSIZE);
}
//输入ffind和replace数组
i=0;
while(i < iCase)
{
gets(szFindStr[i]);
gets(szReplaceStr[i]);
i = i+1;
}
//输入szFinalText字符串
gets(szFinalText);
//处理字符串
szEdit(szFinalText,iCase);
//输出最终处理后的字符串
int iLength = strlen(szFinalText);
if(iLength <= 80)
printf("%s",szFinalText);
else
for(int k = 0; k < 80; k++)printf("%c",szFinalText[k]);
printf("\n");
}
return 0;
}
10115 Automatic Editing
最新推荐文章于 2022-07-17 11:03:19 发布