#include <iostream>#include <string>#include <map>#include <algorithm>using namespace std; struct Info{ int rule; string str1, str2; map<string, string> mymap;};//字串的替換!用庫函數的replace會出錯,第一次還可以,當用到第二次的時候就出錯!我也不知道錯在哪!//找出字串的位置,然後分三段,再進行字串的替換! string myreplace(string dest, string r, int pos1, int pos2){ int i, len; len = dest.length(); string str1, str2; for (i = 0; i < pos1; i++) str1.push_back(dest[i]); for (i = pos2; i < len; i++) str2.push_back(dest[i]); dest.erase(pos1, pos2); dest.clear(); dest += str1; dest += r; dest += str2; return dest;}int main(){ int i, n, len, pos1, pos2; Info info[15]; string tmp1, tmp2, text, substr; while (cin >> n){ if (n == 0) break; cin.get(); for (i = 0; i < n; i++){ getline(cin, tmp1); getline(cin, tmp2); info[i].str1 = tmp1; info[i].str2 = tmp2; info[i].rule = i+1; info[i].mymap[tmp1] = tmp2; } getline(cin, text); for (i = 0; i < n; i++){ while (1){//對每一個rule的進行尋找,到沒有這個字串的時候就退出,再進行下一個字串的尋找,一直到結束! pos1 = text.find(info[i].str1); if (pos1 < 0) break; pos2 = pos1 + info[i].str1.length(); text = myreplace(text, info[i].mymap[info[i].str1], pos1, pos2); } } cout << text << endl; } system("pause");}