https://codeforces.com/problemset/problem/1097/C

C++版本一

题解:首先保证每个字符串只多(和)其中一个,然后匹配就好了

/*
*@Author:   STZG
*@Language: C++
*/
#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<cmath>
#include<list>
#include<map>
#include<set>
//#define DEBUG
#define RI register int
using namespace std;
typedef long long ll;
//typedef __int128 lll;
const int N=1000000+10;
const int MOD=1e9+7;
const double PI = acos(-1.0);
const double EXP = 1E-8;
const int INF = 0x3f3f3f3f;
int t,n,m,k,q,ans;
int a[N];
char str[N];
stack<char>st;
int main()
{
#ifdef DEBUGfreopen("input.in", "r", stdin);//freopen("output.out", "w", stdout);
#endifscanf("%d",&n);memset(a,0,sizeof(a));for(int i=1;i<=n;i++){cin>>str;while (!st.empty()) st.pop();st.push(str[0]);for (int j=1;str[j]!='\0';++j){if (!st.empty()){if (st.top()=='('&&str[j]==')'){st.pop();}else st.push(str[j]);}else st.push(str[j]);}int ls=0,rs=0;while (!st.empty()){char xx=st.top();st.pop();if (xx=='(') ls++;else rs++;}if (ls!=0&&rs!=0){continue;}else if (ls==0&&rs==0){a[500000]++;}else if (rs!=0){a[500000-rs]++;}else a[500000+ls]++;}for(int i=1;i<=500000;i++){ans+=min(a[500000-i],a[500000+i]);}ans+=a[500000]/2;cout << ans << endl;//cout << "Hello world!" << endl;return 0;
}

C++版本二

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
const int mx=5e5+10;
char str[mx];
int num=0;
int sum[mx];
int sum2[mx];
int n;
int ans=0;
stack<char>st;
int main()
{int t;scanf("%d",&t);for (int i=1;i<=t;++i){scanf("%s",str);while (!st.empty()) st.pop();st.push(str[0]);for (int j=1;str[j]!='\0';++j){if (!st.empty()){if (st.top()=='('&&str[j]==')'){st.pop();}else st.push(str[j]);}else st.push(str[j]);}int ls=0,rs=0;while (!st.empty()){char xx=st.top();st.pop();if (xx=='(') ls++;else rs++;}if (ls!=0&&rs!=0){continue;}else if (ls==0&&rs==0){num++;}else if (rs!=0){sum2[rs]++;}else sum[ls]++;}for (int i=1;i<=mx-10;++i){if (sum[i]>sum2[i]){ans+=sum2[i];}else ans+=sum[i];}ans+=num/2;printf("%d\n",ans);
}

Yuhao and a Parenthesis相关推荐

  1. codeforces 1097 Hello 2019

    又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std;char ...

  2. missing closing parenthesis at end of #pragma

    环境为Tornado2.2,VxWorks5.5 编译出现警告如下: warning: missing closing parenthesis at end of #pragma warning: U ...

  3. boost::graph模块实现DFS parenthesis的测试程序

    boost::graph模块实现DFS parenthesis的测试程序 实现功能 C++实现代码 实现功能 boost::graph模块实现DFS parenthesis的测试程序 C++实现代码 ...

  4. ORA-00907:missing right parenthesis缺少右括号

    一,有嵌套查询,并且子查询中用了union all合并两个查询时,前一个查询用了order by,那么会报错并提示ORA-00907:missing right parenthesis缺少右括号: s ...

  5. ORA-00906 missing left parenthesis括号

    Oracle 建表报错:ORA-00906 missing left parenthesis括号     建表语句:create table test(id char,name varchar(1), ...

  6. 【暴力】UVALive - 4882 - Parenthesis

    就不断地扫整个序列,如果发现多余的括号就删除.大概复杂度还是O(n²)左右.如何判断不合法请详见代码. To a computer, there is no difference between th ...

  7. Codeforces 1153 C Serval and Parenthesis Sequence

    题意: 给一个字符串 只包含 '(' . ')' .和 ' ?' 要求改变 '?' 为 '(' 或 ')' 使最终的字符串满足:从第一位开始到任意一位(非最后一位)的字符串不出现形如 '( )'的情况 ...

  8. recurrence relation in parenthesis placing problem

  9. Serval and Parenthesis Sequence

    https://codeforces.com/contest/1153/problem/C 题解:贪心+后缀和 /* *@Author: STZG *@Language: C++ */ #includ ...

最新文章

  1. 卡尔曼滤波— Constant Velocity Model
  2. php的延迟绑定,PHP延迟静态绑定使用方法实例解析
  3. 关于 Twing Hot Link 的一些事
  4. java 判断时间合法_java 中 Date 类型快判断日期是否合法.
  5. tomcat(2)--集群
  6. ElasticStack系列之十六 ElasticSearch5.x index/create 和 update 源码分析
  7. iphone闪退修复工具_苹果中国回应iPhone致命漏洞:不予置评
  8. GEMM算法及优化流程详解
  9. Flutter实现帧动画
  10. koa --- [MVC实现之一]自定义路由读取规则
  11. uac2.0驱动_关闭Vista中令人讨厌的HP驱动程序UAC弹出更新检查
  12. ES2019 的新功能 flat()
  13. sql年月日加减法,计算两个日期之间的天数
  14. linux中vim查看最后五行命令,Linux系统中Vi常用命令及高级应用
  15. Linux学习总结(23)——SSH协议详解
  16. 到底如何调用Async异步函数
  17. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_16-CMS前端工程创建-导入系统管理前端工程...
  18. 沃谈小知识|可“防拆”的远程锁机
  19. QCC高通烧录驱动安装方法
  20. 常平计算机培训班,东莞常平十大CAD培训班排名(如何为初学者学习CAD)

热门文章

  1. ubuntu 无法进入startx_Ubuntu无法进入图形化界面(报错/dev/sda1:clean的解决)
  2. android纹理存储,android纹理文本
  3. oracle查询记录插入,我应该使用哪种查询语法在Oracle数据库中插入记录?
  4. c++ 弧形面如何逆时针排序_环形导轨如何实现拐弯?
  5. mysql对库授权alter_mysql 数据库授权(给某个用户授权某个数据库)
  6. php5.6 交叉编译,Cross-compile - 龙芯开源社区
  7. python remove函数_python中remove函数的用法是什么?
  8. 三十七、Java集合中的Hashtable
  9. 四十五、Redis数据库学习
  10. 四十四、Mysql的命令和PyMysql