Codeforces 723B Text Document Analysis (字符串处理)

B. Text Document Analysis
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Modern text editors usually show some information regarding the document being edited. For example, the number of words, the number of pages, or the number of characters.

In this problem you should implement the similar functionality.

You are given a string which only consists of:

  • uppercase and lowercase English letters,
  • underscore symbols (they are used as separators),
  • parentheses (both opening and closing).

It is guaranteed that each opening parenthesis has a succeeding closing parenthesis. Similarly, each closing parentheses has a preceding opening parentheses matching it. For each pair of matching parentheses there are no other parenthesis between them. In other words, each parenthesis in the string belongs to a matching "opening-closing" pair, and such pairs can't be nested.

For example, the following string is valid: "_Hello_Vasya(and_Petya)__bye_(and_OK)".

Word is a maximal sequence of consecutive letters, i.e. such sequence that the first character to the left and the first character to the right of it is an underscore, a parenthesis, or it just does not exist. For example, the string above consists of seven words: "Hello", "Vasya", "and", "Petya", "bye", "and" and "OK". Write a program that finds:

  • the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses),
  • the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
Input

The first line of the input contains a single integer n (1 ≤ n ≤ 255) — the length of the given string. The second line contains the string consisting of only lowercase and uppercase English letters, parentheses and underscore symbols.

Output

Print two space-separated integers:

  • the length of the longest word outside the parentheses (print 0, if there is no word outside the parentheses),
  • the number of words inside the parentheses (print 0, if there is no word inside the parentheses).
Examples
Input
37
_Hello_Vasya(and_Petya)__bye_(and_OK)
Output
5 4


Input
37
_a_(_b___c)__de_f(g_)__h__i(j_k_l)m__
Output
2 6


Input
27
(LoooonG)__shOrt__(LoooonG)
Output
5 2


Input
5
(___)
Output
0 0


Note

In the first sample, the words "Hello", "Vasya" and "bye" are outside any of the parentheses, and the words "and", "Petya", "and" and "OK" are inside. Note, that the word "and" is given twice and you should count it twice in the answer.


思路:

题意就是给你一串字符串,查询在括号外边的所有字符中能构成的单词中的最大长度(中间有‘_’的单词就被分开了)和括号里边能构成的单词的个数。只需要对“_”、“(”、")"进行分情况讨论即可。需要注意的两个WA点是,如果出现"_"的时候前面是没有单词的,那么它之前的数就不能构成一个单词了;在一个就是当判断字符串的最后一个字符时候要在它continue之前完成maxn的更新。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int main()
{
	int n;
	while(scanf("%d",&n)!=EOF)
	{
		char s[256];
		scanf("%s",s);
		int maxn1=-1;
		int maxn2=0;
		int cnt1=0;
		int cnt2=0;
		int flag=false;
		for(int i=0;i<n;i++)
		{
			if(flag==false&&s[i]!='_'&&s[i]!='('&&s[i]!=')')
			{
				cnt1++;
				if(i==n-1&&cnt1!=0)//WA在这个细节了 
				{
					maxn1=max(maxn1,cnt1);
				}
				continue; 
			} 
			if(flag==false&&s[i]=='_')
			{
				if(cnt1==0)//WA在这个细节了 
				{
	     			cnt1=0;
					continue;	
				}
				else
				{		
					maxn1=max(maxn1,cnt1);
					cnt1=0;
					continue;	
				}
			} 
			if(flag==false&&s[i]=='(')
			{
				if(cnt1==0)
				{
					cnt1=0;
					flag=true;
					continue;
				}
				else
				{
					maxn1=max(maxn1,cnt1);
					cnt1=0;
					flag=true;
					continue;					
				}
			} 
			if(flag==true&&s[i]!='_'&&s[i]!='('&&s[i]!=')')
			{
				cnt2++;
				continue;
			}
			if(flag==true&&s[i]=='_')
			{
				if(cnt2!=0)maxn2++;
				cnt2=0;
				continue;
			}
			if(flag==true&&s[i]==')')
			{
				flag=false;
		    	if(cnt2!=0)maxn2++;
				cnt2=0;
				continue;
			}
		}
		if(maxn1==-1)printf("0 %d\n",maxn2);
		else printf("%d %d\n",maxn1,maxn2);
	}
	return 0;
}

全部评论

相关推荐

原来已经一年了,因为没有加任何实验室没有学长学姐带,再一次偶然的机会下刷到我们学校的牛肉哥,和他聊天之后发现他也没加实验室能进大厂,我就燃起了希望,去年大概&nbsp;4&nbsp;月份找好路线&nbsp;零基础&nbsp;开始学&nbsp;5&nbsp;月背八股和开始刷算法很难受&nbsp;7-8&nbsp;月焦虑躯体化害怕找不到实习&nbsp;9&nbsp;月找到一家像样的小厂去实习了&nbsp;4&nbsp;个月大三上期末考试结束之后&nbsp;1&nbsp;月份回来边实习边准备工作压力很大&nbsp;当时只有字节、百度、商汤的面试,字节三面挂了,百度&nbsp;oc,商汤&nbsp;二面挂(差评&nbsp;无效面试),之后来深圳百度实习之后还是觉得不甘心一直没把算法和八股扔下一直在准备,百度实习的时候&nbsp;mt&nbsp;交给我一个特别重要的工作数据库迁移(特别感谢&nbsp;mt&nbsp;,这个需求学到了很多东西处理了一堆线上问题),本来看着暑期他们面试都很困难,然后听说百度要涨实习薪资(然而&nbsp;5&nbsp;月并没有涨),就想着留在百度吧也懒得面试了,4&nbsp;月&nbsp;20&nbsp;多的时候字节&nbsp;hr&nbsp;打电话约面问我要不要尝试一下询问了&nbsp;1&nbsp;月份三面为啥会挂有没有学习&nbsp;ai&nbsp;知识(因为字节这边后端岗位偏&nbsp;ai),我来到百度之后全面拥抱&nbsp;AI&nbsp;也认识了我的好兄弟&nbsp;X&nbsp;哥,他在百度&nbsp;XX&nbsp;部门&nbsp;Agent&nbsp;实习,他属于是我&nbsp;Agent&nbsp;的启蒙老师,来百度之后一直在了解&nbsp;AI&nbsp;这一块,我就接受了字节的面试,一面的时候&nbsp;20&nbsp;分钟实习拷打然后突然说&nbsp;30&nbsp;分钟代码考核我心就凉了以为是&nbsp;kpi,算法题是手撕高并发安全下的令牌桶限流器,我写了整整&nbsp;80&nbsp;多行代码最后也写出来了,但是从来没看到过出这种题能&nbsp;oc&nbsp;的我也就不管了,后边面试也是很顺利但是流程有点长可能一直在横向吧总结结果是好的!!!感谢这一年努力的自己和遇到的各位互联网大佬分享的知识!!!ps&nbsp;图二纯感慨&nbsp;(觉得🍬请不要喷我)欢迎大家一起交流学习呀!!!!
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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