zoukankan      html  css  js  c++  java
  • Codeforces1097C Yuhao and a Parenthesis 栈

    网址:http://codeforces.com/contest/1097/problem/C

    题意:

    输入一些字符串,只含有左右括号,当且仅当两字符串的合成体左右括号数量相同且最左边是左括号,最右边是右括号时,字符串可组合,字符串只能用一次。求最大组合数。

    题解:

    可以先处理"$()$"的情况。处理字符串中部分回文的情况,可以使用栈解决。

    处理完后:1.如果只有$'('$,与有相同数量的$')'$的串组合。

    如果是空串,空串计数器$+1$,如果两种都有,不可能组合。

    数量就是空串数除$2$取整$+$左右括号数对应的串数的较小值的加和。

    AC代码:

    #include <iostream>
    #include <cstdio>
    #include <stack>
    #include <string>
    #include <cstring>
    #define min(a,b) (a>b?b:a)
    using namespace std;
    const int MAXN = 5e5 + 5;
    int non = 0, l[MAXN], r[MAXN];
    char tmp[MAXN];
    int main()
    {
    	int n;
    	scanf("%d", &n);
    	for (int i = 0; i < n; ++i)
    	{
    		scanf("%s", tmp);
    		int len = strlen(tmp);
    		int ll= 0, rr = 0;
    		stack<char>s;
    		for (int j = 0; j < len; ++j)
    		{
    			if (s.empty())
    				s.push(tmp[j]);
    			else if (s.top() == '(' && tmp[j] == ')')
    				s.pop();
    			else
    				s.push(tmp[j]);
    		}
    		while (!s.empty())
    		{
    			if (s.top() == '(')
    				++ll;
    			else
    				++rr;
    			s.pop();
    		}
    		if (ll + rr == 0)
    			++non;
    		else if (ll > 0 && rr == 0)
    			++l[ll];
    		else if (ll == 0 && rr > 0)
    			++r[rr];
    	}
    	int ans = 0;
    	for (int i = 0; i < MAXN; ++i)
    		ans += min(l[i], r[i]);
    	ans += non / 2;
    	printf("%d
    ", ans);
    	return 0;
    }
    
  • 相关阅读:
    JS-OO-数据属性,访问器属性
    下载php扩展笔记
    php字符串笔记
    include、require、include_once和require_once理解
    http协议笔记
    Git中三种文件状态及其转换
    git add 命令
    / 直接用就可以了 想用,需要用\来转义
    $_POST 变量以及$GLOBALS['HTTP_RAW_POST_DATA']
    Python multiprocessing
  • 原文地址:https://www.cnblogs.com/Aya-Uchida/p/11226211.html
Copyright © 2011-2022 走看看