1. 程式人生 > >CCF-模板生成系統-201509-3

CCF-模板生成系統-201509-3

ras i++ 替換 spa etl getch pla pan c++

主要是string---STL的運用

趁機整理一下erase, find, substr, replace, insert

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 const int N=107;
 4 string str[N];
 5 map <string,string> mapp;
 6 int main ()
 7 {
 8     int m,n; cin>>m>>n; getchar();
 9     for (int i=0;i<m;i++)  
10 getline(cin,str[i]); 11 for (int i=0;i<n;i++) { 12 string s1,s2; 13 cin>>s1; getline(cin,s2); 14 s2=s2.substr(2,s2.size()-3); 15 mapp[s1]=s2; 16 } 17 for (int i=0;i<m;i++) { 18 string s=str[i]; 19 string tmp; int
j=0; 20 while (j<s.size()) { 21 int _s=s.find("{{ ",j); 22 int _e=s.find(" }}",_s); 23 if (_s!=-1&&_e!=-1) { 24 int len=_e-_s-3; 25 string txt=s.substr(_s+3,len); 26 txt=mapp[txt]; 27 s.replace(_s,_e+3
-_s,txt); 28 j=_s+txt.size(); // j=_e+3; 字符串替換後 長度也發生了變化 29 } 30 else break; 31 } 32 cout<<s<<"\n"; 33 } 34 return 0; 35 }

CCF-模板生成系統-201509-3