Codeforces 223A Bracket Sequence [栈]

给一串由(,
), [ ,]构成的字符串,求包含[最多的合法子串

很容易,先把整个字符串丢入栈里处理

栈的每一个元素存两个东西,字符,在字符串中的位置

处理方式为如果是()匹配则直接丢弃,如果是[]匹配则在这个点vis[i]++,然后求vis的前缀和

如果栈空,则说明整个串是合法的,直接输出串

否则,扫描栈中剩下的元素的位置,这几个位置把整个原串切割成几段,这几段肯定是合法的,求这几段中的含[最大的段,可以用前缀和求得,输出这段

游标,左右区间非常容易弄错。有一次手动输出了‘\0‘,导致看起来和答案一样,就是WA

#include <cstdio>
#include <climits>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
char str[111111];
int vis[111111];
int sumvis[111111];
struct STACK{
	char c;
	int pos;
}sta[111111];
int main(){
#ifndef ONLINE_JUDGE
	freopen("/home/rainto96/in.txt","r",stdin);
#endif
	cin>>str;
	int p=-1;
	int len =strlen(str);
	for(int i=0;i<len;i++){
		if(p!=-1){
			if(str[i]==')' && sta[p].c=='('){//直接丢弃
				p--;
				continue;
			}
			if(str[i]==']' && sta[p].c=='[') {//vis[i]++,便于快速求得段中含[的个数
				p--;
				vis[i]++;
				continue;
			}
			sta[++p]=(STACK){str[i],i};
		}else 	sta[++p]=(STACK){str[i],i};
	}
	sumvis[0]=vis[0];
	for(int i=1;i<len;i++)
		sumvis[i]=sumvis[i-1]+vis[i];
        if(p==-1){//如果栈里没有东西,则整个字符串合法,输出该串
                cout<<sumvis[len-1]<<endl;
                cout<<str<<endl;
                return 0;
	}
	int x=-1,y=-1,ans=0;//求栈中剩下元素使得原串分割的段中含[的最多的串
	int ansx=-1,ansy=-1;
	for(int i=0;i<=p;i++){
		y=sta[i].pos;
		int tmpans=sumvis[y-1]-sumvis[x]+(x==0?vis[x]:0);
		if(tmpans>ans){
			ans=tmpans;
			ansx=x+1;
			ansy=y-1;
		}
		x=y;
	}
        y=len;
        int tmpans=sumvis[y-1]-sumvis[x]+(x==0?vis[x]:0);
        if(tmpans>ans){
                ans=tmpans;
                ansx=x+1;
                ansy=y-1;
        }
	cout<<ans<<endl;
	//cout<<x<<endl;
	//cout<<y<<endl;
	if(ans>0){
                for(int i=ansx;i<=ansy;i++)
                        cout<<str[i];
        }
	cout<<endl;
}
时间: 2024-08-06 06:00:35

Codeforces 223A Bracket Sequence [栈]的相关文章

Codeforces Beta Round #5 C. Longest Regular Bracket Sequence 栈/dp

C. Longest Regular Bracket Sequence Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/5/C Description This is yet another problem dealing with regular bracket sequences. We should remind you that a bracket sequence

Codefroces 223A - Bracket Sequence【栈优化】

题目大意: 有一串只含有 "("  ")"  "["  "]" 的序列,问在该序列的 左右括号能分别配对的 所有子串中的含有方括号的个数的最大值,并输出相对应的子串. 做法: 利用一个栈来维护,每次如果有能与栈顶的元素配对的右边括号时,将该元素弹出,如果此时弹出的元素代表方括号,那么记录此时出现的下标.否则将该元素压进栈. 当然我们压进栈的是当前元素的下标.当处理完整个序列之后,栈里面存的就是所有不能配对的括号所处的下标,每两

贪心+stack Codeforces Beta Round #5 C. Longest Regular Bracket Sequence

题目传送门 1 /* 2 题意:求最长括号匹配的长度和它的个数 3 贪心+stack:用栈存放最近的左括号的位置,若是有右括号匹配,则记录它们的长度,更新最大值,可以在O (n)解决 4 详细解释:http://blog.csdn.net/taoxin52/article/details/26012167 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cstring> 9 #include <

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 线段树模拟

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

Codeforces 1132A. Regular Bracket Sequence

原题链接:Codeforces 1132A. Regular Bracket Sequence 题目大意:你有\({cnt}_1,{cnt}_2,{cnt}_3,{cnt}_4\)个"((","()",")(","))",问能否将这些字符串组成一个合法的括号序列. 题解:这一道题,很明显的\({cnt}_2\)是不需要管的,对于第三种情况,它并不改变左右括号的数量差,只有第一.四情况改变,那么,很明显\({cnt}_1={cn

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor

E. Correct Bracket Sequence Editor Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical express

Codeforces 524F And Yet Another Bracket Sequence 哈希

And Yet Another Bracket Sequence 枚举起点, 增加的(肯定在最前面, 增加的)肯定在最后面, 比字典序用hash, 卡了自然溢出.. #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #define fi first #define se second #define mk make_pair #define PLL

cf670E Correct Bracket Sequence Editor

Recently Polycarp started to develop a text editor that works only with correct bracket sequences (abbreviated as CBS). Note that a bracket sequence is correct if it is possible to get a correct mathematical expression by adding "+"-s and "

D - Replace To Make Regular Bracket Sequence

You are given string s consists of opening and closing brackets of four kinds <>, {}, [], (). There are two types of brackets: opening and closing. You can replace any bracket by another of the same type. For example, you can replace < by the bra