UVA - 10115 Automatic Editing

题目大意:找一个字符串中是否有匹配的,如果有就进行替换,如果没有的话就继续和下一匹配,如果有的话,就替换

解题思路:数组存放

#include
#include

void replace(char *str, char *yuan, char *ti) {
	int len1 = strlen(str);
	int len2 = strlen(yuan);
	int len3 = strlen(ti);
	int mark;
	
	for(int i = 0; i < len1; i++)
		if(str[i] == yuan[0]) {// 找到原字符串在总字符串的位置   
			mark = i;
			int j;
			for(j = 0; j < len2; j++)
				if(str[mark + j] != yuan[j])
					break;
			if(j == len2)
				break;
		}
	char temp[10000];
	memset(temp,'\0',sizeof(temp));
	strcpy(temp,str);
	for(int i = 0; i < len3; i++)
		str[mark + i] = ti[i];
	for(int i = mark + len2, j = mark + len3; mark < len1; i++,j++, mark++ )
		str[j] = temp[i];
}

int main() {
	
	int number;
	char yuan[1000][1000];
	char re[1000][1000];
	char str[3000];
	while(scanf("%d", &number) != EOF) {
		if(number == 0)
			break;
		getchar();

		memset(yuan,'\0',sizeof(yuan));
		memset(re,'\0',sizeof(re));
		memset(str,'\0',sizeof(str));

		for(int i = 0; i < number; i++) {
			gets(yuan[i]);
			gets(re[i])	;
		}

		gets(str);
/*		for(int j = 0; j < number; j++)  {
			char *p, *q;
			while(p = strstr(str,yuan[j]), p!=NULL ) {
				q = p + strlen(yuan[j]);
				strcpy(temp,re[j]);
				strcat(temp,q);
				strcpy(p,temp);
			}	
			
		}
*/
		for(int j = 0; j < number; j++)
			while(strstr(str,yuan[j]))
				replace(str,yuan[j],re[j]);
				
		
		printf("%s\n",str);
	}
	return 0;
}


你可能感兴趣的:(ACM-水题)