Codeforces Round #375 (Div. 2) - B

 

题目链接:http://codeforces.com/contest/723/problem/B

题意:给定一个字符串。只包含_,大小写字母,左右括号(保证不会出现括号里面套括号的情况),_分隔开单词。现在要你输出括号外面的单词的最大长度和括号里面出现的单词数目

思路:按照题目模拟就好了。写的比较搓。

#define _CRT_SECURE_NO_DEPRECATE
#include<stdio.h>  
#include<string.h>  
#include<cstring>
#include<algorithm>  
#include<queue>  
#include<math.h>  
#include<time.h>
#include<vector>
#include<iostream>
using namespace std;
typedef long long int LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 300 + 10;
int n;
char str[MAXN];
int main(){
    while (~scanf("%d", &n)){
        scanf("%s", str);
        bool inner = false; int maxlen = 0, cnt = 0;
        for (int i = 0; i<strlen(str); i++){
            if (str[i] == '_'){
                continue;
            }
            if (str[i] == '('){
                int k = i + 1; bool haschar = false;
                for (; k<strlen(str); k++){
                    if (str[k] == ')'){
                        cnt += (haschar ? 1 : 0);
                        i = k; break;
                    }
                    if (str[k] == '_'){
                        cnt += (haschar ? 1 : 0);
                        haschar = false;
                    }
                    else{
                        haschar = true;
                    }
                }
            }
            else{
                int k = i; int tmplen = 0;
                for (; k <= strlen(str); k++){
                    if (k == strlen(str)){
                        maxlen = max(maxlen, tmplen);
                        i = k;
                        break;
                    }
                    else if (str[k] == '_'){
                        maxlen = max(maxlen, tmplen);
                        tmplen = 0;
                        continue;
                    }
                    else if (str[k] == '('){
                        maxlen = max(maxlen, tmplen);
                        i = k - 1;
                        break;
                    }
                    else{
                        tmplen++;
                    }
                }
            }
        }
        printf("%d %d\n", maxlen, cnt);
    }
    return 0;
}

 

posted @ 2016-10-04 09:03  キリト  阅读(155)  评论(0编辑  收藏  举报