Carpenters' Language(类似括号匹配)

 Carpenters' Language

时间限制: 1 Sec  内存限制: 128 MB

题目描述

International Carpenters Professionals Company (ICPC) is a top construction company with a lot of expert carpenters. What makes ICPC a top company is their original language.

The syntax of the language is simply given in CFG as follows:

S -> SS | (S) | )S( | ε
In other words, a right parenthesis can be closed by a left parenthesis and a left parenthesis can be closed by a right parenthesis in this language.

Alex, a grad student mastering linguistics, decided to study ICPC's language. As a first step of the study, he needs to judge whether a text is well-formed in the language or not. Then, he asked you, a great programmer, to write a program for the judgement.

Alex's request is as follows: You have an empty string S in the beginning, and construct longer string by inserting a sequence of '(' or ')' into the string. You will receive q queries, each of which consists of three elements (p,c,n), where p is the position to insert, n is the number of characters to insert and c is either '(' or ')', the character to insert. For each query, your program should insert c repeated by n times into the p-th position of S from the beginning. Also it should output, after performing each insert operation, "Yes" if S is in the language and "No" if S is not in the language.

Please help Alex to support his study, otherwise he will fail to graduate the college.

 

输入

The first line contains one integer q (1≤q≤105) indicating the number of queries, follows q lines of three elements, pi, ci, ni, separated by a single space (1≤i≤q, ci='('or')', 0≤pi≤ length of S before i-th query, 1≤n≤220). It is guaranteed that all the queries in the input are valid.

 

输出

For each query, output "Yes" if S is in the language and "No" if S is not in the language.

 

样例输入

复制样例数据

3
0 ( 10
10 ) 5
10 ) 5

样例输出

No
No
Yes

气人哦,一开始用了int交了已发,错了,然后我用树状数组开了半天的空间。。。虽然树状数组过了,但是第一次改成long long也对的。!!!

题意就是是否可以匹配。

解法:怎么匹配都行,可以'('匹配')',也可以')'匹配'(',好像你加括号进去也就这两种可能,那么只要知道左右括号是否相等就行了。

法一:

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

LL num1, num2;
int t;
LL p, num;
char ch;

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	scanf("%d", &t);
	while(t--){
		scanf("%lld %c %lld", &p, &ch, &num);
		if(ch == '(') num1 += num;
		else num2 += num;
		if(num1 == num2) printf("Yes\n");
		else printf("No\n");
	}

	return 0;
}
/**/

法二:树状数组

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int t;
LL q;
int n;
char ch;
const LL maxx = ((1LL << 20) * 100001);
map<LL, LL> c;


LL lowbit(LL x){
	return x & (-x);
}

void add(LL x, int num){
	//if(!x) c[x] += num, x++;
	while(x <= maxx){
		c[x] += num;
		x += lowbit(x);
	}
}

LL sum(LL x){
	LL t = 0;
	while(x){
		t += c[x];
		x -= lowbit(x);
	}
	return t;
}

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	scanf("%d", &t);
	while(t--){
		scanf("%lld %c %d", &q, &ch, &n);
		if(ch == '(') add(q + 1, n);
		else add(q + 1, -n);
		LL t = sum(q + 105);
		if(t == 0){
			printf("Yes\n");
		}else{
			printf("No\n");
		}
	}

	return 0;
}
/**/

 

全部评论

相关推荐

牛客96931767...:这履历不是在网安横着走啊
大家都开始春招面试了吗
点赞 评论 收藏
分享
02-25 09:55
已编辑
门头沟学院 Java
2.4&nbsp;一面2.6&nbsp;二面2.9&nbsp;三面(hr面)2.13&nbsp;oc1.15号收到面试电话那会就开始准备,因为一开始没底所以选择推迟一段时间面试,之后开始准备八股,准备实习可能会问的东西,这期间hot100过了有六七遍,真的是做吐了快,八股也是背了忘,忘了背,面经也看了很多,虽然最后用上的只有几道题,可是谁知道会问什么呢自从大二上开始学java以来,一开始做外卖,点评,学微服务,大二下五六月时,开始投简历,哎,投了一千份了无音讯,开始怀疑自己(虽然能力确实很一般),后来去到一家小小厂,但是并不能学到什么东西,而且很多东西都很不规范,没待多久便离开,大二暑假基本上摆烂很怀疑自己,大三上因为某些原因开始继续学,期间也受到一俩个中小厂的offer,不过学校不知道为啥又不允许中小厂实习只允许大厂加上待遇不太好所以也没去,感觉自己后端能力很一般,于是便打算转战测开,学习了一些比较简单的测试理论(没有很深入的学),然后十二月又开始继续投,java和测开都投,不过好像并没有几个面试,有点打击不过并没有放弃心里还是想争一口气,一月初因为学校事比较多加上考试便有几天没有继续投,10号放假后便继续,想着放假应该很多人辞职可能机会大一点,直到接到字节的面试,心里挺激动的,总算有大厂面试了,虽然很开心,但同时压力也很大,心里真的很想很想很想进,一面前几天晚上都睡不好觉,基本上都是二三点睡六七点醒了,一面三十几分钟结束,问的都不太难,而且面试官人挺好但是有些问题问的很刁钻问到了测试的一些思想并不是理论,我不太了解这方面,但是也会给我讲一讲他的理解,但是面完很伤心觉得自己要挂了。但是幸运的是一面过了(感谢面试官),两天后二面,问的同样不算难,手撕也比较简单,但也有一两个没答出来,面试官人很好并没有追问,因为是周五进行的二面,没有立即出结果,等到周一才通知到过了,很煎熬的两天,根本睡不好,好在下周一终于通知二面过了(感谢面试官),然后约第二天三面,听别的字节同学说hr面基本上是谈薪资了,但是我的并不是,hr还问了业务相关的问题,不过问的比较浅,hr还问我好像比较紧张,而且hr明确说了还要比较一下,我说我有几家的面试都拒了就在等字节的面试,三面完后就开始等结果,这几天干啥都没什么劲,等的好煎熬,终于13号下午接到了电话通知oc了,正式邮件也同时发了,接到以后真的不敢信,很激动但更重要的是可以松一口气了,可以安心的休息一下了终于可以带着个好消息过年了,找实习也可以稍微告一段落了,虽然本人很菜,但是感谢字节收留,成为忠诚的节孝子了因为问的比较简单,面经就挑几个记得的写一下一面:1.实习项目的难点说一下2.实习中用到了哪些测试方法3.针对抖音评论设计一下测试用例4.手撕:合并两个有序数组二面:1.为什么转测开2.线程进程区别,什么场景适合用哪个3.发送一个朋友圈,从发出到别人看到,从数据流转的角度说一下会经历哪些过程4.针对抖音刷到广告视频设计测试用例5.手撕:无重复字符的最长字串
厂办龚彪:锲而不舍 金石可镂
查看8道真题和解析
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务