hdoj Scaena Felix

Scaena Felix

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 101    Accepted Submission(s): 49

Problem Description

Given a parentheses sequence consist of ‘(‘ and ‘)‘, a modify can filp a parentheses, changing ‘(‘ to ‘)‘ or ‘)‘ to ‘(‘.

If we want every not empty <b>substring</b> of this parentheses sequence not to be "paren-matching", how many times at least to modify this parentheses sequence?

For example, "()","(())","()()" are "paren-matching" strings, but "((", ")(", "((()" are not.

Input

The first line of the input is a integer T, meaning that there are T test cases.

Every test cases contains a parentheses sequence S only consists of ‘(‘ and ‘)‘.

1≤|S|≤1,000.

Output

For every test case output the least number of modification.

Sample Input

3
()
((((
(())

Sample Output

1
0
2

题解:水题。。。。匹配括号的个数。。。

代码:

 1 #include<stdio.h>
 2 #include<stack>
 3 using namespace std;
 4 const int MAXN=1100;
 5 char m[MAXN];
 6 int main(){
 7     int T;
 8     scanf("%d",&T);
 9     while(T--){
10         stack<char>st;
11         scanf("%s",m);
12         int k=0;
13         for(int i=0;m[i];i++){
14             if(m[i]==‘(‘)st.push(m[i]);
15             else if(!st.empty())st.pop(),k++;
16         }
17         printf("%d\n",k);
18     }
19     return 0;
20 }
时间: 2024-10-27 02:56:56

hdoj Scaena Felix的相关文章

hdoj 5479 || bestcoder #57 div 2 A Scaena Felix(模拟)

Scaena Felix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 182    Accepted Submission(s): 85 Problem Description Given a parentheses sequence consist of '(' and ')', a modify can filp a parent

Scaena Felix

Scaena Felix Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description Given a parentheses sequence consist of '(' and ')', a modify can filp a parentheses, changing '(' to ')' or ')' to '('. If we want ever

hdu 5479 Scaena Felix (好坑的简单题)

坑点较多,能过以下数据估计就可以了. 样例:4()(((((()))))()(()))((()(((输出:1024 首先,遍历一遍,计算“(”和“”的个数,取最小值记为ans. 然后,判断有没有“)(”这种字串,有的话,最终的串要转换为“...)))))(((((...”这种形式. 用两个数组:dp1从左到右计算“)”的个数前缀和:dp2从右往左计算“(”的个数后缀和. 遍历一遍,ans=min( ans , dp1[i] + dp2[i+1]).(详见代码) /* Title :Scaena

栈——括号匹配

Scaena Felix Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 492    Accepted Submission(s): 214 Problem Description Given a parentheses sequence consist of '(' and ')', a modify can filp a paren

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

POJ Xiangqi 4001 &amp;&amp; HDOJ 4121 Xiangqi

题目链接(POJ):http://poj.org/problem?id=4001 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=4121 Xiangqi Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1108   Accepted: 299 Description Xiangqi is one of the most popular two-player boa

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

HDOJ 4901 The Romantic Hero

DP....扫两遍组合起来 The Romantic Hero Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 547    Accepted Submission(s): 217 Problem Description There is an old country and the king fell in love with a

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return