文章标题

问题 H: Text Document Analysis

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

  • 列表内容

题目描述
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)
37
a(b___c)de_f(g)__h__i(j_k_l)m
27
(LoooonG)shOrt(LoooonG)
样例输出
5 4
2 6
5 2


语言 C++ 代码如下

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

int main()
{
    int n;
    while(cin>>n)   //多组输入 
    {
        char a[n+1];  //字符数组 ,b数组记录括号,c记录横线,d记录横线间差值 
        int b[300],c[300],d[300],k=0,h=0,g=0,t,sum=0; 
        for(int i=0;i<n;i++)
        {
            cin>>a[i];   
            if(a[i]=='('||a[i]==')')  //判断括号,并存储它出现时的序列值 
            {
                b[k]=i;  
                k++;
            }
        }
        t=k;  //记录b数组的长度 
        k=0;
        for(int i=0;i<n;i++)    
        {
            if(i==b[k])
            {
                c[h]=b[k];  //为了记录横线,顺便还有括号 
                h++;     //每次记录后,序列加一 
                k++; 
                i=b[k];  //碰见括号,把i的值移到括号‘)’后一位 
                c[h]=b[k];
                h++;
                k++;
                continue;  //结束本次循环 
            }
            if(a[i]=='_')  
            {
                c[h]=i;
                h++;    
            }

            if(i==n-1&&a[n-1]!='_')   //当到最后一位时,应判断是否为横线 
            {
            c[h]=n; h++;      
         //如果不是要记录后一位的序列,以便得知前面的单词长度
           }
        }
        for(int i=0;i<h;i++)  
        {
            if(i==0)   //i==0是个特殊值,要单独考虑 
            {
                d[g]=c[i];  //记录第一个横线前的的单词长度 
                g++;         //别忘加一 
            }

            if(i>0)   //大于零时,后面的横线的序列号减去前面的横线 
            {                  //差值即为单词长度 
                d[g]=c[i]-c[i-1]-1;
                g++;
            }
            if(a[c[i]]=='(')  //碰见括号,要往后挪一位 
            i++;
        }
        int m=0,r=b[0];
        sort(d,d+g);    //sort函数为排序函数,头文件名<algorithm> 
            cout<<d[g-1]<<" ";  //输出最大值 
        for(int i=0;i<n;i++)   //判断括号里面的单词个数 
        {
            if(a[i]=='(')    //代表进入括号 
            {
                for(int j=i+1;j<n;j++) 
                {
                    if(a[j]==')')  //判断括号结束条件 
                    {
                        i=j;
                        break;   //终止循环 
                    }
                    if(j==i+1&&a[j]!='_')  //判断括号内第一个字符 
                    sum++;
                    if(a[j]=='_'&&a[j+1]!=')') //为了判断__即横线连续的情况 
                    {
                        for( m=j+1;m<n;m++)
                        {
                            if(a[m]==')')  
                            {
                                j=m-1; 
                                break;
                            }
                            if(a[m]!='_')  //直到出现字母为止 
                            {
                                j=m;
                                sum++;
                                break;
                            }
                        }
                    }
                }

            }
        }

    cout<<sum<<endl;  //输出单词数 

    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值