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

坑点较多,能过以下数据估计就可以了。

样例:
4
()
((((
(())
)))()(()))((()(((
输出:
1
0
2
4

首先,遍历一遍,计算“(”和“”的个数,取最小值记为ans。

然后,判断有没有“)(”这种字串,有的话,最终的串要转换为“...)))))(((((...”这种形式。

用两个数组:dp1从左到右计算“)”的个数前缀和;dp2从右往左计算“(”的个数后缀和。

遍历一遍,ans=min( ans , dp1[i] + dp2[i+1])。(详见代码)

/*

Title :Scaena Felix
Status:AC

By wf,2015 9 27

*/
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#define FOR(i,s,t) for(int i = (s) ; i <= (t) ; ++i )

typedef long long ll;
using namespace std;

const int inf=0x3f3f3f3f;
const int maxn=1e3+5;

int t;
string s;
int dp1[maxn],dp2[maxn];
int main()
{
	cin>>t;
	while(t--){
		cin>>s;
		memset(dp1,0,sizeof dp1);
		memset(dp2,0,sizeof dp2);
		int len=s.length();
		bool ok=false;
		int ans;
		for(int i=0;i<len-1;++i){
			if(s[i]==‘)‘ && s[i+1]==‘(‘)ok=true;
		}
		int n1=0,n2=0;
		for(int i=0;i<len;++i){
			if(s[i]==‘(‘)n1++;
			else n2++;
		}
		ans=min(n1,n2);
		if(ok){
			for(int i=0;i<len;++i){
				if(i==0)dp1[i]=0;
				else dp1[i]=dp1[i-1];

				if(s[i]==‘(‘)dp1[i]++;
			}
			for(int i=len-1;i>=0;i--){
				if(i==len-1)dp2[i]=0;
				else dp2[i]=dp2[i+1];

				if(s[i]==‘)‘)dp2[i]++;
			}
			for(int i=0;i<len-1;++i){
				ans=min(ans,dp1[i]+dp2[i+1]);
			}
		}
		printf("%d\n",ans );
	}
	return 0;
}

  

时间: 2024-08-07 12:44:57

hdu 5479 Scaena Felix (好坑的简单题)的相关文章

hdu 1234 开门人和关门人(简单题)

开门人和关门人 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10658    Accepted Submission(s): 5434 Problem Description 每天第一个到机房的人要把门打开,最后一个离开的人要把门关好.现有一堆杂乱的机房签 到.签离记录,请根据记录找出当天开门和关门的人. Input 测试输入的第一行

HDU 1010 Tempter of the Bone DFS 简单题 注意剪枝

题意:一只小狗要刚好在t时刻从起点都到终点,问可不可以. 注意剪枝. 1 #include<cstdio> 2 #include<cstring> 3 #include<cmath> 4 using namespace std; 5 int maze[9][9]; 6 bool vis[9][9]; 7 int n,m,t; 8 bool ans; 9 struct Point 10 { 11 int x,y; 12 }; 13 int dx[4]={0,0,-1,1}

hdu 4463 Outlets Prim 次小生成树 简单题

Outlets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2200    Accepted Submission(s): 1028 Problem Description In China, foreign brand commodities are often much more expensive than abroad. T

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

HDU 1025 Constructing Roads In JGShining&#39;s Kingdom   LIS 简单题 好题 超级坑

Constructing Roads In JGShining's Kingdom Problem Description JGShining's kingdom consists of 2n(n is no more than 500,000) small cities which are located in two parallel lines. Half of these cities are rich in resource (we call them rich cities) whi

hdu 1202The calculation of GPA (简单题+坑)

The calculation of GPA Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 18748    Accepted Submission(s): 4331 Problem Description 每学期的期末,大家都会忙于计算自己的平均成绩,这个成绩对于评奖学金是直接有关的.国外大学都是计算GPA(grade point a

HDU 1048 What Is Your Grade? (简单模拟)

 What Is Your Grade? Problem Description "Point, point, life of student!" This is a ballad(歌谣)well known in colleges, and you must care about your score in this exam too. How many points can you get? Now, I told you the rules which are used in

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 parent

hdu 2200 Eddy&#39;s AC难题(简单数学。。)

题意: N个人,每个人AC的题数都不一样. Eddy想从中选出一部分人(或者全部)分成两组.必须满足第一组中的最小AC数大于第二组中的最大AC数. 问共有多少种不同的选择方案. 思路: 简单数学.. 代码: ll C(int n,int x){ ll ans=1; rep(i,1,x){ ans = ans*(n+1-i)/i; } return ans; } int main(){ int n; while(cin>>n){ ll ans = 0; rep(i,2,n){ ans += (C