这题的数据需要注意一下,题意不是很清楚,可以去uva的论坛里看看别人贴的数据
C++语言:
001 /* 002 +++++++++++++++++++++++++++++++++++++++ 003 author: chm 004 +++++++++++++++++++++++++++++++++++++++ 005 */ 006 007 #include <map> 008 #include <set> 009 #include <list> 010 #include <queue> 011 #include <cmath> 012 #include <stack> 013 #include <bitset> 014 #include <cstdio> 015 #include <cctype> 016 #include <vector> 017 #include <cstdlib> 018 #include <cstring> 019 #include <fstream> 020 #include <sstream> 021 #include <iomanip> 022 #include <iostream> 023 #include <algorithm> 024 025 using namespace std; 026 027 FILE * fin = stdin; 028 FILE * fout = stdout; 029 const int max_size = 10086; 030 char str [ max_size ]; 031 char tmpstr [ max_size ]; 032 char original [ max_size ][ 400 ]; 033 char replacestr [ max_size ][ 400 ]; 034 035 int main() 036 { 037 #ifndef ONLINE_JUDGE 038 freopen( "c: \\ in.txt" , "r" , stdin); 039 fout = fopen( "c: \\ garage \\ out.txt" , "w"); 040 #endif 041 int rules; 042 char * ptr; 043 int len1 , len2; 044 045 while( scanf( "%d \n " , & rules) && rules) 046 { 047 for( int i = 0; i < rules; ++ i) 048 { 049 fgets( original [ i ], sizeof( original [ i ]), stdin); 050 fgets( replacestr [ i ], sizeof( replacestr [ i ]), stdin); 051 original [ i ][ strlen( original [ i ]) - 1 ] = '\0'; 052 replacestr [ i ][ strlen( replacestr [ i ]) - 1 ] = '\0'; 053 } 054 fgets( str , sizeof( str ), stdin); 055 056 for( int i = 0; i < rules; ++ i) //for each rule 057 { 058 ptr = strstr( str , original [ i ]); 059 len1 = strlen( replacestr [ i ]); 060 len2 = strlen( original [ i ]); 061 062 while( ptr) 063 { 064 strcpy( tmpstr , ptr + len2); 065 /* 066 **** 067 beba boat 068 behind the goat 069 */ 070 071 /* 072 **** 073 bababa boat 074 beba boat 075 */ 076 strcpy( ptr + len1 , tmpstr); 077 strncpy( ptr , replacestr [ i ], len1); 078 ptr = strstr( ptr , original [ i ]); 079 } 080 } 081 fprintf( fout , "%s" , str); 082 } 083 084 #ifndef ONLINE_JUDGE 085 fclose( fout); 086 system( "c: \\ garage \\ check.exe"); 087 system( "notepad c: \\ garage \\ out.txt"); 088 #endif 089 return 0; 090 } 091 /* 092 4 093 ban 094 bab 095 096 baba 097 be 098 099 ana 100 any 101 102 ba b 103 hind the g 104 105 banana boat 106 babana boat 107 bababa boat 108 beba boat 109 behind the goat 110 111 1 112 t 113 sh 114 toe or top 115 0 116 --------------------------------- 117 behind the goat 118 shoe or shop 119 120 */