Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟
B. Text Document Analysis
题目连接:
http://codeforces.com/contest/723/problem/B
Description
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).
Sample Input
37
_Hello_Vasya(and_Petya)_bye(and_OK)
Sample Output
5 4
Hint
题意
问你括号外面的单词,最长的是多长。
括号内一共有多少个单词。
题解:
模拟一下就好了嘛,写的比较烦,可能。
代码
#include<bits/stdc++.h>
using namespace std;
int n;
string s;
int solve(int l,int r)
{
int tmp = 0;
for(int i=l;i<=r;i++)
{
if(s[i]!='('&&s[i]!='_'&&s[i]!=')')
{
if(i==0||s[i-1]=='('||s[i-1]==')'||s[i-1]=='_')
tmp++;
}
}
return tmp;
}
int solve2(int l,int r)
{
int tmp = 0;
int ans = 0;
for(int i=l;i<=r;i++)
{
if(s[i]!='('&&s[i]!='_'&&s[i]!=')')tmp++;
else tmp=0;
ans=max(tmp,ans);
}
return ans;
}
int main()
{
cin>>n>>s;
int ans = 0;
int flag = 0;
for(int i=0;i<s.size();i++)
{
if(s[i]=='(')flag=i+1;
if(s[i]==')')ans+=solve(flag,i-1),flag=0;
}
int tmp = 0,ans2=0;
flag = 0;
for(int i=0;i<s.size();i++)
{
if(s[i]==')')flag=i+1;
else if(s[i]=='(')ans2=max(solve2(flag,i-1),ans2);
}
ans2=max(ans2,solve2(flag,s.size()-1));
cout<<ans2<<" "<<ans<<endl;
}
Codeforces Round #375 (Div. 2) B. Text Document Analysis 模拟的更多相关文章
- Codeforces Round #375 (Div. 2) A B C 水 模拟 贪心
A. The New Year: Meeting Friends time limit per test 1 second memory limit per test 256 megabytes in ...
- Codeforces Round #375 (Div. 2)
A. The New Year: Meeting Friends 水 #include <set> #include <map> #include <stack> ...
- Codeforces Round #375 (Div. 2) ABCDE
A - The New Year: Meeting Friends 水 #include<iostream> #include<algorithm> using namespa ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #375 (Div. 2) - D
题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...
- Codeforces Round #375 (Div. 2) - C
题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...
- Codeforces Round #375 (Div. 2) - B
题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...
- Codeforces Round #375 (Div. 2) - A
题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...
- Codeforces Round #375 (Div. 2) F. st-Spanning Tree 生成树
F. st-Spanning Tree 题目连接: http://codeforces.com/contest/723/problem/F Description You are given an u ...
随机推荐
- bzoj千题计划235:bzoj2448: 挖油
http://www.lydsy.com/JudgeOnline/problem.php?id=2448 一遍过,嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎,O(∩_∩)O~ 题意是最小化最大值 设计区间dp dp[i ...
- Lua程序设计(四)面向对象类继承
1.类继承 ①代码 Sharp = { _val = } --① 父类 function Sharp:new() local new_sharp = { } self.__index = self - ...
- Comparing Differently Trained Models
Comparing Differently Trained Models At the end of the previous post, we mentioned that the solution ...
- git 学习小记
话说 git 出了已经很久了,可是我一直没用过.其实也不是没用过,只不过在 github 上下载东西那根本就不是在用 git,只是单纯的HTTP下载而已.我们公司用的是 svn,所以我只会一点点svn ...
- 20155227 2016-2017-2 《Java程序设计》第五周学习总结
20155227 2016-2017-2 <Java程序设计>第五周学习总结 教材学习内容总结 语法与继承架构 使用try...catch JVM会尝试执行try区块中的程序代码,如果发生 ...
- 【Windows编程】大量病毒分析报告辅助工具编写
解决重复劳动 是否在分析单个病毒时很爽,分析N个病毒写报告很机械的情况.. 1)样本下载多个文件,这些文件写报告时要加上这些文件的MD5 2)写报告时明明是17个MD5,实际样本有18个的情况.不知道 ...
- python自动安装mysql5.7【转】
#!/usr/bin/env python import os import sys import re base_dir = '/opt/software/mysql-5.7.17-linux-gl ...
- js async await 终极异步解决方案
既然有了promise 为什么还要有async await ? 当然是promise 也不是完美的异步解决方案,而 async await 的写法看起来更加简单且容易理解. 回顾 Promise Pr ...
- java虚拟机规范(se8)——java虚拟机结构(一)
本文翻译自:https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html 第二章 虚拟机结构 本文档描述了一个抽象的虚拟机规范,并不描述 ...
- findHomography和perspectiveTransform
opencv中的两个函数,之前一直不明白这俩函数到底是要干嘛的. 得到两帧图像中的特征点后,就可以用findHomography得到单应性矩阵. 得到单应性矩阵后,可以 (1)根据相应的计算方法,由前 ...