poj 1572 Automatic Editing

來源:互聯網
上載者:User
#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");}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.