64,666
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<map>
using namespace std;
int string_replace(string &s1, const string &s2, const string &s3)
{
string::size_type pos = 0;
string::size_type a = s2.size();
string::size_type b = s3.size();
while ((pos = s1.find(s2,pos)) != string::npos)
{
s1.replace(pos, a, s3);
pos += b;
}
return 0;
}
int main()
{
int m = 0 ,n = 0 ,count = 0,countn = 0;
vector<string>temp;
vector<string>no;
map<string,string>info;
map<string,string>::iterator it;
cin>>m>>n;
getchar();
while(count!=m)
{
string res;
getline(cin,res);
//if (!res.empty())
//{
temp.push_back(res);
count++;
//}
}
while(countn!=n)
{
string res;
getline(cin,res);
if (!res.empty())
{
string nre,re;
int index = 0;
for(int i = 0;i < res.length()-1;i++)
{
if(res[i] == '"')
{
index = i;
}
}
nre = res.substr(0,index-1);
re = res.substr(index+1,res.length()-index-2);
info.insert(pair<string, string>("{{ "+nre+" }}", re));
countn++;
}
}
//cout << "input finished!\n";
//找到无定义的变量,字典值设为空串
for(int i = 0;i < m;i++)
{
string res = temp[i];
if(res.length() == 0) continue;
if(res.length() < 7) continue;
for(int j = 0;j <res.length()-4;j++)
{
int s_pos = 0,e_pos = 0,mcount = 0;
string argu;
if(res[j]=='{'&&res[j+1]=='{')
{
s_pos = j+3;
for(int k = s_pos;k < res.length();k++)
{
if(res[k] == ' ')
{
e_pos = k;
break;
}
}
}
argu = res.substr(s_pos,e_pos-s_pos);
for(it = info.begin();it!=info.end();it++)
{
if(argu != it->first)
{
mcount+=1;
}
}
if(mcount == info.size())
{
if(!argu.empty())
{
argu = "{{ "+argu+" }}";
no.push_back(argu);
}
}
else
{
continue;
}
}
}
for(it = info.begin();it!=info.end();it++)
{
for(int i = 0;i < m;i++)
{
string_replace(temp[i],it->first,it->second);
}
}
for(int i = 0;i < no.size();i++)
{
for(int j = 0;j < m;j++)
{
string_replace(temp[j],no[i],"");
}
}
for(int i = 0;i < m;i++)
{
cout<<temp[i]<<endl;
}
return 0;
}