poj 1572 Automatic Editing

来源:互联网 发布:克里斯保罗16年数据 编辑:程序博客网 时间:2024/05/19 18:16
#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");}

原创粉丝点击