第一题
题解:合并两个集合,放于同一数组排序,输出时采用set可以去掉相同数。
#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
int n,m;
while(cin >> n >> m)
{
int a[20010]={0};
int t;
for(int i=0;i<n;i++)
{
cin >> t;
a[i] = t;
}
for(int j=0;j<m;j++)
{
cin >>t;
a[n+j] = t;
}
sort(a,a+n+m);
set<int> s;
for(int i=0;i<m+n;i++)
{
if(s.find(a[i])!=s.end())
continue;
if(i==0)
{
cout << a[i];
s.insert(a[i]);
}
else
{
cout << " " << a[i];
s.insert(a[i]);
}
}
cout << endl;
}
return 0;
}
第二题
题解:采用栈,进行模拟;使用队列将输出答案压入队列,最后输出。
#include <iostream>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int main()
{
int n;
string in,out;
while(cin >> n)
{
cin >> in >> out;
stack<char> s;
queue<string> q;
int o=0;
int i=0;
for(int j=0;j<n*2;j++)
{
if(!s.empty()&&s.top()==out[o])
{
s.pop();
o++;
q.push("out");
}
else if(i<n)
{
s.push(in[i++]);
q.push("in");
}
}
if(s.empty())
{
cout << "Yes." << endl;
while(!q.empty())
{
cout << q.front() << endl;
q.pop();
}
}
else
cout << "No." << endl;
cout << "FINISH" << endl;
}
return 0;
}
第三题
题解: 利用set集合的特点,仅保留不同的字符串。
#include <iostream>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
using namespace std;
int main()
{
int n;
while(cin >> n)
{
string t;
set<string> s;
for(int i=0;i<n;i++)
{
cin >> t;
s.insert(t);
}
cout << s.size() << endl;
}
return 0;
}
第四题
题解:使用全排序 next_permutation(a.begin(),a.end()); 从小到大列举。以及vector存储。
#include <iostream>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
using namespace std;
vector<int> a;
int n,m;
int main()
{
while(~scanf("%d%d",&n,&m))
{
for(int i=1;i<=n;i++)
a.push_back(i);
for(int i=0;i<m-1;i++)
next_permutation(a.begin(),a.end());
for(int i=0;i<n;i++)
{
if(i==0)
cout << a[i];
else
cout << " " << a[i];
}
cout << endl;
a.clear();
}
return 0;
}
第五题
题解: 使用map字典,对字符串进行处理。
#include <iostream>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
using namespace std;
map<string,string> mp;
int main()
{
string str;
cin >> str;
string a,b;
while(1)
{
cin >> a;
if(a=="END")
break;
cin >> b;
mp[b] = a;
}
map<string,string>::iterator it;
str="";
getchar();
while(1)
{
getline(cin,a);
if(a=="START") continue;
if(a=="END")
break;
for(int i=0; i<a.size(); i++)
{
if(a[i]>='a'&&a[i]<='z')
{
str+=a[i];
if(i==a.size()-1)
{
it = mp.find(str);
if(it!=mp.end())
{
cout << it->second;
}
else
cout << str;
str = "";
}
}
else
{
it = mp.find(str);
if(it!=mp.end())
{
cout << it->second;
}
else
cout << str;
cout << a[i];
str = "";
}
}
cout << endl;
}
return 0;
}
第六题
题解: 栈的应用。
#include <iostream>
#include <set>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
using namespace std;
map<string,string> mp;
int main()
{
int t=1;
while(1)
{
stack<char> s;
string str;
getline(cin,str);
if(str[0]=='-') break;
int l = str.size();
int cnt=0;
for(int i=0;i<l;i++)
{
if(str[i]==' ') continue;
if(!s.empty())
{
if(str[i]=='{') s.push('{');
else s.pop();
}
else
{
if(str[i]=='{') s.push('{');
else{
cnt++;
str[i]='{';
s.push('{');
}
}
}
int ans = s.size()/2+cnt;
cout << t <<". " << ans << endl;
t++;
}
return 0;
}