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 every not empty substring 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.
If we want every not empty substring 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 .
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
Source
/************************************************************************/
附上该题对应的中文题
Scaena Felix
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
问题描述
给定一个由'('和')'组成的字符串,一次修改可以翻转字符串中的一个字符,将'('变成')'或者将')'变成'('。
如果要让这个字符串中任意一个非空子串都不是括号匹配串,至少要修改多少次?
比如"()","(())","()()" 是括号匹配串, 但"((", ")(", "((()" 不是。
输入描述
输入文件包含多组数据,第一行为数据组数T。 每一组数据为一个字符串S,S只由'('和')'构成。 1≤∣S∣≤1,000.
输出描述
对每组数据输出至少需要修改的字符数。
输入样例
3 () (((( (())
输出样例
1 0 2
/****************************************************/
出题人的解题思路:
Scaena Felix
题目要求每一个子串都不是括号匹配串,实际上只要使得括号匹配的单元“()”串不是该串的子串即可。所以最终串肯定是连续数个')'以及连续数个'(',统计出其中代价最小的即可。
其实在做这个题的时候,还轻松加愉快地以为只要记录'('出现的个数以及')'的个数,输出小的那个即为所求
结果。。。orz,被hack了,排名又掉了,一把辛酸泪,都怪考虑得太少,像")))((("这样的非空子串就不需要修改
好吧,按出题人的意思来,我们暴力一下就好了,对于每个i(0~strlen(s)),记录[0,i)中不是')'的个数与[i,strlen(s))中不是'('的个数之和,取最小值即可。
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<queue>
#include<stack>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<stdlib.h>
#include<cmath>
#include<string>
#include<algorithm>
#include<iostream>
#define exp 1e-10
using namespace std;
const int N = 1005;
const int inf = 1000000000;
const int mod = 2009;
char s[N];
int main()
{
int t,i,j,c,Min,l;
scanf("%d",&t);
while(t--)
{
Min=inf;
scanf("%s",s);
l=strlen(s);
for(i=0;i<=l;i++)
{
for(c=j=0;j<i;j++)
if(s[j]!=')')
c++;
for(;j<l;j++)
if(s[j]!='(')
c++;
Min=min(Min,c);
}
printf("%d\n",Min);
}
return 0;
}
菜鸟成长记