Advertisement

牛客网 编程题 解析(栈结构)

阅读量:

双顶栈结构中,首个栈的顶端即为题目所指的光标位置。在执行栈的弹出操作时,需注意判断栈是否为空。

复制代码
    #include<iostream>
    #include<bits/stdc++.h>
    
    using namespace std;
    
    const int N = 1e6 + 10;
    
    stack<int> c, st1, st2;
    int q, x;
    int s[N], f[N];
    
    int main()
    {
    	while(scanf("%d", &q) != EOF){
    		st1 = c;
    		st2 = c;
    		f[0] = -1e7;
    		s[0] = 0;
    		for(int i = 0; i < q; i ++ ){
    			char op;
    			cin>>op;
    			if(op == 'I'){
    				scanf("%d", &x);
    				st1.push(x);
    				s[st1.size()] = s[st1.size() - 1] + x;//记录前缀和 
    				f[st1.size()] = max(f[st1.size() - 1], s

全部评论 (0)

还没有任何评论哟~