【数据结构模拟题】

【数据结构模拟题】

*都是简单题!!!

【栈模拟】

只要看到两两匹配可消去:想到栈模拟*

tb的字符串问题

https://ac.nowcoder.com/acm/contest/90072/B

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef pair<int,int> PII;
typedef long long ll;
ll abss(ll a){return a>0?a:-a;}
ll max_(ll a,ll b){return a>b?a:b;}
ll min_(ll a,ll b){return a<b?a:b;}
bool cmpll(ll a,ll b){return a>b;}
const int N=1e6+10;
int n;
string s;
int st[N],tt=-1;
signed main(){
      ios::sync_with_stdio(0);
      cin.tie(0);
      cout.tie(0);
      cin>>n;
      cin>>s;
      /*
      f 1  c 2 ->1和2配对
      t 3  b 4 ->3和4配对
      废料都是5
      */
      int ans=0;
      for(int i=0;i<n;i++){
            if(s[i]=='f'){
                  st[++tt]=1;
            }
            else if(s[i]=='t'){
                  st[++tt]=3;
            }
            else if(s[i]=='c'){
                  //每次只能取出来1个
                  if(st[tt]==1){
                        ans++;
                        tt--;
                  }
                  else{
                        st[++tt]=2;
                  }
            }
            else if(s[i]=='b'){
                  if(st[tt]==3){
                        ans++;
                        tt--;
                  }
                  else{
                        st[++tt]=4;
                  }
            }
            else{
                  st[++tt]=5;
            }
      }
      ans=n-ans*2;
      cout<<ans;
      return 0;
}

小苯的数字消除

https://ac.nowcoder.com/acm/contest/104637/C
只需要改一半该改的字符->答案为栈空间/2
注意可以留一个字符->不用管多出的那个

#include<bits/stdc++.h>
using namespace std;
#define endl '\n'
typedef pair<int,int> PII;
typedef long long ll;
ll abss(ll a){return a>0?a:-a;}
ll max_(ll a,ll b){return a>b?a:b;}
ll min_(ll a,ll b){return a<b?a:b;}
bool cmpll(ll a,ll b){return a>b;}
//栈模拟即可
int n;
string s;
int t;
void solve(){
      cin>>n;
      cin>>s;
      stack<int> q;
      for(int i=0;i<n;i++){
            int tmp=s[i]-'0';
            if(q.empty()) q.push(tmp);
            else{
                  if(q.top()==tmp) q.pop();
                  else q.push(tmp);
            }
      }
      int ans=q.size()/2;
      cout<<ans<<endl;
}
signed main(){
      ios::sync_with_stdio(0);
      cin.tie(0);
      cout.tie(0);
      cin>>t;
      while(t--) solve();
      return 0;
}

【双端队列deque模拟】

Chimpanzini Bananini

https://codeforces.com/contest/2094/problem/G
注意反转数组:直接开两个deque 一个插头一个插尾 要反转直接换

int q;
void solve(){
    cin>>q;
    deque<int> que1,que2;//que1和que2是反过来的
    /*
    1.循环移位:头/尾弹掉插另一头
    2.反转:【直接交换】que1和que2
    3.在队尾加新数
    */
    ll ans1=0;
    ll ans2=0;
    ll sum=0;
    ll h=0;
    while(q--){
        int s;
        cin>>s;
        if(s==1){
            int tt=que1.back();
            que1.pop_back();
            ll res=h*(ll)tt;
            ans1+=(sum-res);
            que1.push_front(tt);

            int ttt=que2.front();
            que2.pop_front();
            ll ress=(ll)ttt*h;
            ans2+=(ress-sum);
            que2.push_back(tt);
        }
        else if(s==2){
            swap(ans1,ans2);
            swap(que1,que2);
        }
        else if(s==3){
            int k;
            cin>>k;
            h++;
            que1.push_back(k);
            ans1+=(ll)k*h;
            que2.push_front(k);
            ans2+=(sum+k);
            sum+=k;
        }
        cout<<ans1<<endl;
    }
}

Large Queue

https://atcoder.jp/contests/abc413/tasks/abc413_c
双端队列可以头插也可以尾插->适合处理头部不弹出,只修改的问题

int n;
int q;
deque<P64> que;
void solve(){
    cin>>q;
	while(q--){
		int op;
		cin>>op;
		if(op==1){
			i64 c,x;
			cin>>c>>x;
			que.push_back({c,x});
		}
		else{
			i64 k;
			cin>>k;
			i64 ans=0;
			while(k && que.size()){
				auto t=que.front();
				if(k>=t.fi){
					que.pop_front();
					k-=t.fi;
					ans+=t.fi*t.sc;
				}
				else{
					que.pop_front();
					ans+=k*t.sc;
					t.fi-=k;
					que.push_front(t);
					k=0;
				}
			}
			cout<<ans<<endl;
		}
	}
}
posted @ 2025-02-08 11:03  White_ink  阅读(15)  评论(0)    收藏  举报