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 模拟的更多相关文章

  1. 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 ...

  2. Codeforces Round #375 (Div. 2)

    A. The New Year: Meeting Friends 水 #include <set> #include <map> #include <stack> ...

  3. Codeforces Round #375 (Div. 2) ABCDE

    A - The New Year: Meeting Friends 水 #include<iostream> #include<algorithm> using namespa ...

  4. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  5. Codeforces Round #375 (Div. 2) - D

    题目链接:http://codeforces.com/contest/723/problem/D 题意:给定n*m小大的字符矩阵.'*'表示陆地,'.'表示水域.然后湖的定义是:如果水域完全被陆地包围 ...

  6. Codeforces Round #375 (Div. 2) - C

    题目链接:http://codeforces.com/contest/723/problem/C 题意:给定长度为n的一个序列.还有一个m.现在可以改变序列的一些数.使得序列里面数字[1,m]出现次数 ...

  7. Codeforces Round #375 (Div. 2) - B

    题目链接:http://codeforces.com/contest/723/problem/B 题意:给定一个字符串.只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词 ...

  8. Codeforces Round #375 (Div. 2) - A

    题目链接:http://codeforces.com/contest/723/problem/A 题意:在一维坐标下有3个人(坐标点).他们想选一个点使得他们3个到这个点的距离之和最小. 思路:水题. ...

  9. 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 ...

随机推荐

  1. java中文件的I/O操作

    java中文件的读写操作 (一) (1)java中文件的字节转成字符读操作 FileInputStream fStream = new FileInputStream("test.txt&q ...

  2. ELK Kafka json to elk

    Logstash配置     input { kafka { zk_connect => "127.0.0.1:2181" topic_id => "clus ...

  3. 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知

    [源码下载] 重新想象 Windows 8 Store Apps (67) - 后台任务: 推送通知 作者:webabcd 介绍重新想象 Windows 8 Store Apps 之 后台任务 推送通 ...

  4. .net视图中日期格式化

    昨天在做一个功能,要在界面上按照规定的格式显示一个时间,如果直接在expression那里格式化的话(如下:) @Html.DisplayFor(c => Convert.ToDateTime( ...

  5. asp.net-(含:模拟登陆,照片列表)

    一.画好用户登录界面 同时换下请求的地址. 获取用户信息及判断代码插入位置: 一.画好用户登录界面 同时换下请求的地址. 获取用户信息及判断代码插入位置: <%@ WebHandler Lang ...

  6. 使用JS制作一个鼠标可拖的DIV(四)——缩放

    原理与鼠标拖动 DIV 相同. 下面就先实现一个在DIV的右上角显示一个小正方形(类似). 当鼠标按下并拖动时,DIV会以要拖动的元素的左下角的坐标点定位,根据鼠标的拖动,按比例的扩大或缩小. 一.思 ...

  7. Devexpress 之gridControl双击行事件

    MouseDown事件 protected internal void gridControl1_MouseDown(object sender, MouseEventArgs e) { DevExp ...

  8. Java对象引用

    1.对象的强.软.弱和虚引用 在JDK 1.2以前的版本中,若一个对象不被任何变量引用,那么程序就无法再使用这个对象.也就是说,只有对象处于可触及(reachable)状态,程序才能使用它.从JDK ...

  9. synchronized和进程间通信(转)

    关于JAVA多线程同步 JAVA多线程同步主要依赖于若干方法和关键字 1  wait方法: 该方法属于Object的方法,wait方法的作用是使得当前调用wait方法所在部分(代码块)的线程停止执行, ...

  10. shell脚本获取文件中key/value的小方法

    方法有N种,awk.sad.grep.cut... 以上几种方式不写了,就写两个不太常用到的. 废话少说,直接上代码: cat a.txt aa.gif=aaaa.gif bb.gif=bbbb.gi ...