sgu 538. Emoticons 水

本文介绍了一种用于聊天应用中的Emoticons高亮显示规则,并提供了一个示例程序来计算给定字符串中作为Emoticons一部分的括号数量。该规则确保了只有符合特定条件的括号才被视为Emoticons的一部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

538. Emoticons
Time limit per test: 1 second(s)
Memory limit: 262144 kilobytes
input: standard
output: standard



A berland national nanochat Bertalk should always stay up-to-date. That's why emoticons highlighting was decided to be introduced. As making emoticons to be highlighted is not exactly the kind of task one performs everyday but this task had to be done as soon as possible, the following simple rule was decided to be introduced: a round opening or closing bracket  be considered part of an emoticon if:
  • this is an opening bracket and there exists the nearest bracket following to the right. The nearest round bracket to the right should be a closing bracket and there shouldn't be anything between the brackets but spaces and Latin letters,
  • or else it can be a closing bracket and there exists the nearest round bracket following to the left. The nearest round bracket to the left should be an opening bracket. Besides, there shouldn't be anything between the brackets but spaces and Latin letters.


If a bracket doesn't satisfy the conditions, it is considered a part of an emoticon. For example, let's consider the string "
Hi:) (it is me) I have bad news:-((
". In the string only the brackets that outline "
it is me
" aren't emoticons. Note that an opening bracket immediatelly followed by a closing bracket, i.e. "
()
", are not parts of emoticons by definition.

Your task is to print the number of brackets that are parts of emoticons in the given string.

Input
The input data consist of a single non-empty string. The length of the string does not exceed 105 characters. The string consists of lowercase and uppercase Latin letters, spaces, round brackets and punctuation marks: "
-
", "
:
", "
,
", "
;
". The string does not begin with and does not end with a space.

Output
Print a single number — the required number of brackets that are part of emoticons.

Example(s)
sample input
sample output
Hi:) (it is me) I have bad news:-((
3 

sample input
sample output
((two plus two equals four)) 
2 


#include <bits/stdc++.h>

using namespace std;

const int M=2e5+5;

char s[100005];

int main()
{
    scanf("%[^\n]",s);
    int len=strlen(s);

    int ans=0;
    int f=-1;
    for(int i=0;i<len;i++){
        if(s[i]=='('||s[i]==')'){
            ans++;
        }
        if(f==-1){
            if(s[i]=='('){
                f=1;
            }
        }
        else{
            if(s[i]=='-'||s[i]==':'||s[i]==','||s[i]==';'){
                f=-1;
            }
            else if(s[i]==')'){
                f=-1;
                ans-=2;
            }
        }

    }

    printf("%d",ans);

    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值