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).
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.
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).
37 _Hello_Vasya(and_Petya)__bye_(and_OK)
5 4
37 _a_(_b___c)__de_f(g_)__h__i(j_k_l)m__
2 6
27 (LoooonG)__shOrt__(LoooonG)
5 2
5 (___)
0 0
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.
题意:给定长度为n的字符串,单词以符号分隔,找出括号内单词数量和括号外单词的最大长度;
分类讨论括号内外的情况:
括号内如果遇到')'就退出括号,单词标记=0;遇到'_'就增加一个单词,单词标记=0;遇见字母就把单词标记=1;
括号外如果遇到'('就进入括号,保存取前面单词的最大长度,单词标记=0;遇到'_'就取前面单词最大长度,单词标记=0;遇到字母就单词标记=1,长度累加;
括号外遇到符号时记得将单词长度初始化;
最后如果结束遍历字符串后,单词标记还在,那么再取一次最大长度(针对以单词结尾的情况)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char s[260];
int main(){
int n;
scanf("%d",&n);
scanf("%s",s);
int len=0,lenMax=0,cnt=0;
bool wfin=0,wfout=0,inp=0;
for(int i=0;s[i];i++){
if(inp){//in ()
if(s[i]==')'){
inp=0;
if(wfin){
cnt++;
wfin=0;
}
}
else if(s[i]=='_'){
if(wfin){
cnt++;
wfin=0;
}
}
else {
wfin=1;
}
}
else{//out ()
if(s[i]=='('){
inp=1;
if(wfout){
lenMax=max(len,lenMax);
wfout=0;
len=0;
}
}
else if(s[i]=='_'){
if(wfout){
lenMax=max(len,lenMax);
len=0;
wfout=0;
}
}
else{
len++;
wfout=1;
}
}
// cout<<"i:"<<i<<" "<<"lenMax:"<<lenMax<<" "<<"len:"<<len<<" "<<"cnt:"<<cnt<<endl;
// system("pause");
}
if(wfout) lenMax=max(len,lenMax);
printf("%d %d\n",lenMax,cnt);
}
简化版= =。。因为有好多重复代码
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
char s[260];
int main(){
int n;
scanf("%d",&n);
scanf("%s",s);
int len=0,lenMax=0,cnt=0;
bool wfin=0,wfout=0,inp=0;
for(int i=0;s[i];i++){
if(inp){//in ()
if(s[i]==')'||s[i]=='_'){
if(s[i]==')') inp=0;
if(wfin){
cnt++;
wfin=0;
}
}
else {
wfin=1;
}
}
else{//out ()
if(s[i]=='('||s[i]=='_'){
if(s[i]=='(') inp=1;
if(wfout){
lenMax=max(len,lenMax);
wfout=0;
len=0;
}
}
else{
len++;
wfout=1;
}
}
}
if(wfout) lenMax=max(len,lenMax);
printf("%d %d\n",lenMax,cnt);
}