90分代码,用string自带的函数会超时。
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=1e2+5,INF=0x3f;
string s[maxn];
int main()
{
int n,m;
cin>>m>>n;
getchar();
for(int i=0;i<m;i++)
{
getline(cin,s[i]);
//cout<<s[i]<<endl;
}
for(int i=0;i<n;i++)
{
string s1,s2,s3;
cin>>s1;
getline(cin,s2);
s3=s2.substr(2,s2.size()-3);
//cout<<s3<<endl;
//cout<<s1<<" "<<s2<<endl;
for(int j=0;j<m;j++)
{
while(1)
{
int cur=s[j].find("{{ "+s1+" }}");
if(cur!=s[j].npos)
{
s[j].erase(cur,6+s1.size());
s[j].insert(cur,s3);
}
else break;
}
//cout<<s[j]<<endl;
}
}
for(int j=0;j<m;j++)
{
while(1)
{
int cur1=s[j].find("{{ "),cur2=s[j].find(" }}");
if(cur1!=s[j].npos&&cur2!=s[j].npos)
{
s[j].erase(s[j].begin()+cur1,s[j].begin()+cur2+3);
}
else break;
}
}
for(int j=0;j<m;j++)
cout<<s[j]<<endl;
return 0;
}
100分y总代码
#include <iostream>
#include <cstring>
#include <algorithm>
#include <unordered_map>
#include <vector>
using namespace std;
int n, m;
vector<string> strs;
unordered_map<string, string> vars;
int main()
{
cin >> n >> m;
getchar(); // 过滤掉第一行的回车
while (n -- )
{
string str;
getline(cin, str);
strs.push_back(str);
}
while (m -- )
{
string key, value;
cin >> key;
char c;
while (c = getchar(), c != '\"');
while (c = getchar(), c != '\"') value += c;
vars[key] = value;
}
for (auto& str: strs)
{
for (int i = 0; i < str.size();)
if (i + 1 < str.size() && str[i] == '{' && str[i + 1] == '{')
{
int j = i + 3;
string key;
while (str[j] != ' ' || str[j + 1] != '}' || str[j + 2] != '}')
key += str[j ++ ];
cout << vars[key];
i = j + 3;
}
else cout << str[ i ++ ];
cout << endl;
}
return 0;
}
作者:yxc
链接:https://www.acwing.com/activity/content/code/content/872378/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。