您的位置:首页 > 其它

Codeforces Round #459 (Div. 2) C-The Monster (贪心)

2018-02-17 21:30 316 查看
C. The Monstertime limit per test1 secondmemory limit per test256 megabytesinputstandard inputoutputstandard outputAs Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Christmas lights (he can turn them on and off with his mind). He can't directly tell his mom where he is, because the monster that took him to the Upside Down will know and relocate him.

Thus, he came up with a puzzle to tell his mom his coordinates. His coordinates are the answer to the following problem.A string consisting only of parentheses ('(' and ')') is called a bracket sequence. Some bracket sequence are called correct bracket sequences. More formally:Empty string is a correct bracket sequence.
if s is a correct bracket sequence, then (s) is also a correct bracket sequence.
if s and t are correct bracket sequences, then st (concatenation of s and t) is also a correct bracket sequence.
A string consisting of parentheses and question marks ('?') is called pretty if and only if there's a way to replace each question mark with either '(' or ')' such that the resulting string is a non-empty correct bracket sequence.Will gave his mom a string s consisting of parentheses and question marks (using Morse code through the lights) and his coordinates are the number of pairs of integers (l, r) such that 1 ≤ l ≤ r ≤ |s| and the string slsl + 1... sr is pretty, where si is i-th character of s.Joyce doesn't know anything about bracket sequences, so she asked for your help.InputThe first and only line of input contains string s, consisting only of characters '(', ')' and '?' (2 ≤ |s| ≤ 5000).OutputPrint the answer to Will's puzzle in the first and only line of output.ExamplesinputCopy
((?))
output
4
inputCopy
??()??
output
7
NoteFor the first sample testcase, the pretty substrings of s are:"(?" which can be transformed to "()".
"?)" which can be transformed to "()".
"((?)" which can be transformed to "(())".
"(?))" which can be transformed to "(())".
For the second sample testcase, the pretty substrings of s are:"??" which can be transformed to "()".
"()".
"??()" which can be transformed to "()()".
"?()?" which can be transformed to "(())".
"??" which can be transformed to "()".
"()??" which can be transformed to "()()".
"??()??" which can be transformed to "()()()".
【题意】
给定(,),?  问号可代替( 或 ) 问能匹配多少个()
【思路】
O(n^2)  枚举边,  用 T  表示 ( 的数量  用cnt 记录 ? 的数量,  先把? 看做 ) 
遇到 (  T++ 
遇到 ) T--
遇到 ?  T-- ,cnt++
当T=0 说明 可以比配一个( )
当 T<0, cnt >0  说明 ? 此时可以代替 ( 处理,   所以  T+=2  相当于两个 (,cnt--;
当 T<0 cnt < 0  说明 无法匹配 break

【代码实现】#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
string str;
int ans=0;
cin>>str;
int t=0,cnt=0;
int len=str.length();
for(int i=0;i<len;i++)
{
t=0,cnt=0;
for(int j=i;j<len;j++)
{
if(str[j]=='(')
t++;
else if(str[j]==')')
t--;
else
t--,cnt++;

if(t==0)
ans++;
else if(t<0&&cnt>0)
{
t+=2;
cnt--;
}
else if(t<0&&cnt<=0)
break;
}
}
cout<<ans<<endl;
return 0;
}

1
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: