STL在竞赛中的应用


第一题

在这里插入图片描述
题解:合并两个集合,放于同一数组排序,输出时采用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;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值