Uva--10115 (字符串处理)
2014-06-01 00:08:41
题意 & 思路:循环匹配、替换字符串。呵呵,又是一道典型的字符串处理,scanf %c A之,一开始理解为按单词匹配,后在才发现不是这样(fs : ba g rs : hind the g)。
#include <cstdio> #include <cstring> #include <iostream> using namespace std; int main(){ int n; char fs[10][85],rs[10][85],rlen[10],flen[10],sen[260]; while(scanf("%d",&n) == 1 && n){ getchar(); for(int i = 0; i < n; ++i){ char t; int cnt = 0; while(scanf("%c",&t) == 1){ if(t == '\n'){ fs[i][cnt] = '\0'; flen[i] = strlen(fs[i]); break; } else fs[i][cnt++] = t; } cnt = 0; while(scanf("%c",&t) == 1){ if(t == '\n'){ rs[i][cnt] = '\0'; rlen[i] = strlen(rs[i]); break; } else rs[i][cnt++] = t; } } int len,cnt = 0; char t,temp[260]; while(scanf("%c",&t) == 1){ if(t == '\n'){ sen[cnt] = '\0'; len = strlen(sen); break; } else{ sen[cnt++] = t; } } int tag; for(int j = 0; j < n; ++j){ do{ tag = 0; for(int i = 0; i < len; ++i){ if(strncmp(sen + i,fs[j],flen[j]) == 0){ tag = 1; int tk,tcnt = 0; for(tk = 0; tk < i; ++tk) temp[tcnt++] = sen[tk]; for(tk = 0; tk < rlen[j]; ++tk) temp[tcnt++] = rs[j][tk]; for(tk = i + flen[j]; tk < len; ++tk) temp[tcnt++] = sen[tk]; temp[tcnt] = '\0'; strcpy(sen,temp); len = strlen(sen); break; } } }while(tag != 0); } printf("%s\n",sen); } return 0; }