题解
注意下细节……
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <fstream>
#include <map>
using namespace std;
vector<string> tem;
int n, k;
int main()
{
#ifdef LOCAL
freopen("data.in", "r", stdin);
#endif // LOCAL
cin >> n >> k;
string line;
cin.ignore();
for(int i = 0; i < n; ++i){
getline(cin, line);
tem.push_back(line);
}
string key, val;
map<string, string> mp;
for(int i = 0; i < k; ++i){
cin >> key;
getline(cin, val);
val.erase(0, 2);
val.erase(val.length() - 1, 1);
mp[key] = val;
}
for(int i = 0; i < n; ++i)
{
size_t pos1, pos2, pre = 0;
while(1)
{
pos1 = tem[i].find("{{", pre);
if(pos1 == string::npos) break;
pos2 = tem[i].find("}}", pos1);
if(pos2 == string::npos) break;
string key = tem[i].substr(pos1 + 3, pos2 - pos1 - 4);
tem[i].replace(pos1, key.length() + 6, mp.count(key) ? mp[key] : "");
pre = pos1 + key.length(); //note
}
cout << tem[i] << endl;
}
return 0;
}