Yuhao and a Parenthesis
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相关推荐
- codeforces 1097 Hello 2019
又回来了.. A - Gennady and a Card Game 好像没什么可说的了. #include<bits/stdc++.h> using namespace std;char ...
- missing closing parenthesis at end of #pragma
环境为Tornado2.2,VxWorks5.5 编译出现警告如下: warning: missing closing parenthesis at end of #pragma warning: U ...
- boost::graph模块实现DFS parenthesis的测试程序
boost::graph模块实现DFS parenthesis的测试程序 实现功能 C++实现代码 实现功能 boost::graph模块实现DFS parenthesis的测试程序 C++实现代码 ...
- ORA-00907:missing right parenthesis缺少右括号
一,有嵌套查询,并且子查询中用了union all合并两个查询时,前一个查询用了order by,那么会报错并提示ORA-00907:missing right parenthesis缺少右括号: s ...
- ORA-00906 missing left parenthesis括号
Oracle 建表报错:ORA-00906 missing left parenthesis括号 建表语句:create table test(id char,name varchar(1), ...
- 【暴力】UVALive - 4882 - Parenthesis
就不断地扫整个序列,如果发现多余的括号就删除.大概复杂度还是O(n²)左右.如何判断不合法请详见代码. To a computer, there is no difference between th ...
- Codeforces 1153 C Serval and Parenthesis Sequence
题意: 给一个字符串 只包含 '(' . ')' .和 ' ?' 要求改变 '?' 为 '(' 或 ')' 使最终的字符串满足:从第一位开始到任意一位(非最后一位)的字符串不出现形如 '( )'的情况 ...
- recurrence relation in parenthesis placing problem
- Serval and Parenthesis Sequence
https://codeforces.com/contest/1153/problem/C 题解:贪心+后缀和 /* *@Author: STZG *@Language: C++ */ #includ ...
最新文章
- 卡尔曼滤波— Constant Velocity Model
- php的延迟绑定,PHP延迟静态绑定使用方法实例解析
- 关于 Twing Hot Link 的一些事
- java 判断时间合法_java 中 Date 类型快判断日期是否合法.
- tomcat(2)--集群
- ElasticStack系列之十六 ElasticSearch5.x index/create 和 update 源码分析
- iphone闪退修复工具_苹果中国回应iPhone致命漏洞:不予置评
- GEMM算法及优化流程详解
- Flutter实现帧动画
- koa --- [MVC实现之一]自定义路由读取规则
- uac2.0驱动_关闭Vista中令人讨厌的HP驱动程序UAC弹出更新检查
- ES2019 的新功能 flat()
- sql年月日加减法,计算两个日期之间的天数
- linux中vim查看最后五行命令,Linux系统中Vi常用命令及高级应用
- Linux学习总结(23)——SSH协议详解
- 到底如何调用Async异步函数
- 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_16-CMS前端工程创建-导入系统管理前端工程...
- 沃谈小知识|可“防拆”的远程锁机
- QCC高通烧录驱动安装方法
- 常平计算机培训班,东莞常平十大CAD培训班排名(如何为初学者学习CAD)
热门文章
- ubuntu 无法进入startx_Ubuntu无法进入图形化界面(报错/dev/sda1:clean的解决)
- android纹理存储,android纹理文本
- oracle查询记录插入,我应该使用哪种查询语法在Oracle数据库中插入记录?
- c++ 弧形面如何逆时针排序_环形导轨如何实现拐弯?
- mysql对库授权alter_mysql 数据库授权(给某个用户授权某个数据库)
- php5.6 交叉编译,Cross-compile - 龙芯开源社区
- python remove函数_python中remove函数的用法是什么?
- 三十七、Java集合中的Hashtable
- 四十五、Redis数据库学习
- 四十四、Mysql的命令和PyMysql