【UVA】12504 Updating a Dictionary(STL)
题目
分析
第一次用stringstream,真TMD的好用
代码
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
getchar();//回车
while(n--)
{
string s1,s2;
getline(cin,s1);
getline(cin,s2);
for(int i=0;i<s1.length();i++)
if(!isalpha(s1[i]) && !isdigit(s1[i])) s1[i]=' ';
for(int i=0;i<s2.length();i++)
if(!isalpha(s2[i]) && !isdigit(s2[i])) s2[i]=' ';
int l1=0,l2=0;
map<string,string> m;
map<string,string>::iterator it;
vector<string> c,d,e;
stringstream ss(s1),ss2(s2);
string a,b;
while(ss>>a)
{
ss>>b; m[a]=b; l1++;
}
while(ss2>>a)
{
ss2>>b; l2++;
if(!m.count(a)) c.push_back(a);
else if(m[a]!=b) d.push_back(a);
if(m.count(a))
{
it=m.find(a);
m.erase(it);
}
}
sort(d.begin(),d.end());
sort(c.begin(),c.end());
if(c.size())
{
cout<<"+";
for(int i=0;i<c.size();i++)
{
cout<<c[i];
if(i==c.size()-1) cout<<endl;
else cout<<",";
}
}
if(m.size())
{
cout<<"-";
for(map<string,string>::iterator i=m.begin();i!=m.end();i++)
{
if(i!=m.begin()) cout<<',';
cout<<i->first;
}
cout<<endl;
}
if(d.size())
{
cout<<"*";
for(int i=0;i<d.size();i++)
{
cout<<d[i];
if(i==d.size()-1) cout<<endl;
else cout<<',';
}
}
if(!c.size()&&!d.size()&&!m.size()) puts("No changes");
cout<<endl;
}
return 0;
}