UVa 10115 - Automatic Editing

来源:互联网 发布:淘宝宝贝下架时间 编辑:程序博客网 时间:2024/05/21 22:55

题目:给你一些字符串的替换关系,以及一个句子。按顺序替换,输出最后结果。

分析:字符串。按照替换顺序依次替换(这个替换用过之后,就不再使用),每个替换可能出现多次。

            这里注意,如果当前串中有多个可被当前单词替换的位置,只替换最前面的那个,

            下次用本次生成的串替换,而不是整体一次性替换。

说明:注意数据清空。

#include <iostream>#include <cstdlib>#include <cstring>#include <cstdio>using namespace std;char word1[20][84];char word2[20][84];char texts[260];/* 寻找单词 */int find_word( char *a, char *b ){for ( int i = 0 ; a[i] ; ++ i )if ( a[i] != b[i] )return 0;return 1;}/* 替换单词 */void replace_word( char *a, int id ){char buf[256];memset( buf, 0, sizeof(buf) );int move = 0,save = 0;while ( a[move] ) {if ( find_word( word1[id], a+move ) ) {strcpy( buf+save, word2[id] );move += strlen(word1[id]);save += strlen(word2[id]);break;}else buf[save ++] = a[move ++];}while ( a[move] )buf[save ++] = a[move ++];strcpy( a, buf );}int main(){int n;while ( ~scanf("%d",&n) && n ) {getchar();for ( int i = 0 ; i < n ; ++ i ) {gets(word1[i]);gets(word2[i]);}gets(texts);for ( int i = 0 ; i < n ; ++ i )for ( int j = 0 ; j < 260 ; ++ j )replace_word( texts, i );printf("%s\n",texts);}return 0;}

0 0
原创粉丝点击