文章目录
Problem A: STL——灵活的线性表
原题链接
AC源码
#include<iostream>
#include<string>
#include<vector>
using namespace std;
int main()
{
string ss;
int a,b,c;
int i,j;
vector<int> ap;
while(cin>>ss)
{
if(ss=="Build")
{
cin>>a>>b;
ap.assign(a,b);
}
if(ss=="Modify")
{
cin>>a>>b;
ap[a-1]=b;
}
if(ss=="Insert")
{
cin>>a>>b>>c;
ap.insert(ap.begin()+a-1,ap.begin()+b-1,ap.begin()+c);
}
if(ss=="Erase")
{
cin>>a>>b;
ap.erase(ap.begin()+a-1,ap.begin()+b);
}
if(ss=="Print")
{
cin>>a>>b;
for(i=a-1;i<=b-1;i++)
{
cout<<"["<<i+1<<"]:"<<ap[i];
if(i!=b-1)
cout<<" ";
if(i==b-1)
cout<<endl;
}
}
}
return 0;
}
Problem B: STL——集合运算
原题链接
AC源码
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
void print(const set<int> &t)
{
set<int>::iterator it;
cout << "{";
for(it = t.begin(); it != t.end(); it++)
if(it == t.begin()) cout << *it;
else cout << ", " << *it;
cout << "}" << endl;
}
int main()
{
int t;
cin>>t;
int i=0;
while(t--)
{
cout<<"Case# "<<++i<<":"<<endl;
set<int>s1;
set<int>s2;
set<int>tmp1,tmp2,tmp3,tmp4,tmp5;
int m;
cin>>m;
for(int i=0;i<m;i++)
{
int tmp;
cin>>tmp;
s1.insert(tmp);
}
int k;
cin>>k;
for(int i=0;i<k;i++)
{
int tmp;
cin>>tmp;
s2.insert(tmp);
}
cout<<"A = ";
print(s1);
cout<<"B = ";
print(s2);
set_union(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(tmp1,tmp1.begin()));
cout<<"A u B = ";
print(tmp1);
set_intersection(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmp2, tmp2.begin()));
cout<<"A n B = ";
print(tmp2);
set_difference(s1.begin(), s1.end(), s2.begin(), s2.end(), inserter(tmp3, tmp3.begin()));
cout<<"A - B = ";
print(tmp3);
set_difference(tmp1.begin(), tmp1.end(), s1.begin(), s1.end(), inserter(tmp4, tmp4.begin()));
cout<<"SA = ";
print(tmp4);
set_difference(tmp1.begin(), tmp1.end(), s2.begin(), s2.end(), inserter(tmp5, tmp5.begin()));
cout<<"SB = ";
print(tmp5);
}
return 0;
}
Problem C: STL——Jerry的问题
原题链接
AC源码
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main()
{
int a;
while(cin>>a)
{
set<int> s1,s2,s3;
if(a)
{
s1.insert(a);
while(cin>>a&&a)
{
s1.insert(a);
}
}
while(cin>>a&&a)
{
s2.insert(a);
}
set_difference(s1.begin(),s1.end(),s2.begin(),s2.end(),inserter(s3,s3.begin()));
if(s3.empty())
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
Problem D: STL——表达式求值
原题链接
AC源码
#include<iostream>
#include<stack>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
double tp;
char b;
stack<double> dx;
stack<char> cx;
for(int i=1;i<=n;i++)
{
cin>>tp>>b;
if(!dx.empty())
{
if(cx.top() == '-')
tp=-tp;
else if(cx.top()=='*')
{
tp*=dx.top();
dx.pop();
}
}
dx.push(tp);
cx.push(b);
}
tp=0;
while(!dx.empty())
{
tp+=dx.top();
dx.pop();
}
cout<<fixed<<setprecision(2)<<tp<<endl;
}
return 0;
}
Problem E: STL——括号匹配
原题链接
AC源码
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
string c;
int i;
while(cin>>c)
{
stack<int> kh;
//push!
if(c[0]=='(') // 0
{
kh.push(0);
}
else // 1
{
cout<<"NO"<<endl;
continue;
}
for(i=1;i<c.length();i++)
{
if(c[i] == '(')
kh.push(1);
else if(!kh.empty())
kh.pop();
else break;
}
//detect!
if(i == c.length()&&kh.empty())
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
return 0;
}
Problem F: STL——字典
原题链接
AC代码
#include<map>
#include<string>
#include<iostream>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
int i,n;
string w,p;
map<string,string> d;
while(cin>>n)
{
d.clear();
while(n--)
{
cin>>w;
cin>>p;
d[p]=w;
}
cin>>n;
while(n--)
{
string tmp;
cin>>tmp;
if(d.count(tmp)!=0)
cout<<d[tmp]<<endl;
else cout<<"eh"<<endl;
}
}
return 0;
}
Problem G: STL——水果店
原题链接
AC源码
#include<iostream>
#include<map>
#include<algorithm>
#include<string>
using namespace std ;
int main()
{
int n;
while(cin>>n)
{
map<string,int>m;
while(n--)
{
string s;
int num;
cin>>s>>num;
m[s] += num;
}
map<string,int> ::iterator it;
for(it=m.begin();it != m.end();it++)
{
cout<<it->first<<":"<<it->second<<endl;
}
}
return 0;
}
Problem H: STL——字符串排序
原题链接
AC源码
#include<iostream>
#include<string>
#include<vector>
#include<set>
#include<stack>
#include<algorithm>
using namespace std;
int main()
{
int n;
cin>>n;
int j;
string ss[n];
for(j=0;j<n;j++)
cin>>ss[j];
sort(ss,ss+n);
for(j=0;j<n;j++)
cout<<ss[j]<<endl;
return 0;
}