首页 > 其他分享 >B. YetnotherrokenKeoard

B. YetnotherrokenKeoard

时间:2023-12-10 21:22:54浏览次数:26  
标签:int s1 namespace YetnotherrokenKeoard cin 小写字母

一道数据结构题。这题需要用到两个栈分别存储大写字母和小写字母以配合删除操作。

主要代码:

#include<bits/stdc++.h>
using namespace std;
typedef pair<char,int> Pos; 
int main(){
    int n;
    cin>>n;
    while (n--){
        vector<Pos> a,b;
        string s,s1;
        cin>>s;
        for (int i=0;i<s.size();i++){
            if (s[i]!='b' && s[i]!='B'){
                if (s[i]>='A' && s[i]<='Z') a.push_back(Pos(s[i],i));
                else b.push_back(Pos(s[i],i));
            }
            else {
                if (s[i]=='b' && !b.empty()) b.pop_back();
                else if (s[i]=='B' && !a.empty())a.pop_back();
            }
        }
        int m=a.size(),m1=0,n1=0,n=b.size();
        while (m1<m && n1<n){
            if (a[m1].second<b[n1].second){cout<<a[m1].first;m1++;}
            else {cout<<b[n1].first;n1++;}
        }
        while (m1<m){cout<<a[m1].first;m1++;}
        while (n1<n){cout<<b[n1].first;n1++;}
        cout<<endl;
    }
    return 0;
}

 

标签:int,s1,namespace,YetnotherrokenKeoard,cin,小写字母
From: https://www.cnblogs.com/purple123/p/17893245.html

相关文章