题意

一串括号字符串,里面存在一些‘?’,其中‘?’既可以当作 '(' 又可以当作 ')' ,计算有多少对(l,r),在s中[sl,s(l+1),s(l+2),.....sr],内的括号是匹配的。n=strlen(s)<=5000。

分析

这个题还是卡了很久的,我果然是很菜的。

错误思路
一开始的时候想,用一个变量cur表示还未匹配的左括号 '(' ,用变量num记录‘?’的数量,‘?’尽量当右括号使用,在判断(l,r)是否匹配的时候从左往右扫,遇到'(' 的时候cur++,遇到 ')'cur--,如果最左边的字符是'?'那么直接将其变成'('  num--,cur++;因为它如果是')'不再有作用。当cur==num的时候说明当前长度是匹配的。如果num>cur,且(num-cur)%2==0则说明问好可以和‘(’匹配,剩下的‘?’自己互相匹配。

错误代码

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=+;
char s[maxn];
int n;
int main(){
scanf("%s",s);
n=strlen(s);
int ans=;
for(int i=;i<n;i++){
int cur=,num=;
if(s[i]==')')continue;
else cur++;
for(int j=i+;j<n;j++){
bool jud=;
if(s[j]=='(')cur++;
if(s[j]==')')cur--;
if(s[j]=='?')num++;
if(cur==&&num==)jud=;
else if(cur==&&num){cur++;num--;}
if(cur==num)jud=;
else if(num>cur&&(num-cur)%==)jud=;
if(jud)ans++;
}
}
cout<<ans<<endl;
return ;
}

思路纠正:

上面一开始我考虑最左边的'?'只能被当作(使用,可以在深入考虑一下,如果当前?的数量大于(的数量,那么多的?就只能被当作(使用否则没法匹配。这样可以消除对于右边的影响 "(??(()"也就是‘?’当作‘)’来匹配左边的‘(’。

正确代码:

 #include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int maxn=+;
char s[maxn];
int n;
int main(){
scanf("%s",s);
n=strlen(s);
int ans=;
for(int i=;i<n;i++){
int cur=,num=;
if(s[i]==')')continue;
else cur++;
for(int j=i+;j<n;j++){
bool jud=;
if(s[j]=='(')cur++;
if(s[j]==')')cur--;
if(s[j]=='?')num++;
if(cur==&&num==)jud=;
else if(cur==&&num){cur++;num--;}
if(cur==num)jud=;
else if(num>cur&&(num-cur)%==)jud=;
if(jud)ans++;
}
}
cout<<ans<<endl;
return ;
}

codeforce 459DIV2 C题的更多相关文章

  1. codeforce 461DIV2 F题

    题意 题目给出n,k,要求找出一个1到n的子集,(a,b)的对数等于k:(a,b)满足a<b且b%a==0: 分析 还记不记得求素数的时候的欧拉筛!对就那样!如果把每个数字看作一个点的话,可以通 ...

  2. codeforce 461DIV2 E题

    题意 有n棵树排成一排,每个树上都有c[i]只小鸟,只有站在树下才可以召唤小鸟,在i-th树下召唤k(k<=c[i])只小鸟需要消耗cost[i]*k的法力值,但是每召唤一只小鸟可以将法力值的上 ...

  3. codeforce 462DIV2 C题

    题意 给出一个只含有1和2的序列,有n个元素,可以选择一段区间进行翻转操作,求再反转后的最大非递减子序列的长度 分析 太菜了只想出了N^2的做法.序列只有1和2,那么每个非递减子序列都会有一个分界点, ...

  4. codeforce 460DIV2 D题

    感觉这个题不错,对拓扑排序有了更深的了解,用两种拓扑排序都写了些试试. dfs #include <cstdio> #include <algorithm> #include ...

  5. Two progressions CodeForce 125D 思维题

    An arithmetic progression is such a non-empty sequence of numbers where the difference between any t ...

  6. codeforce 457DIV2 C题

    题意 你需要构造一个n个点m条边的无向有权图,要求这个图的MST中边权的和与从1到n的最短路长度都为素数 分析 可以想到这样一种贪心,在i到i+1直接连一条边,这样最短路和MST都会是同样的一些边.只 ...

  7. codeforce 457DIV2 B题

    题意:  题目给出两个整数n,k,(n<=10^18,k<=10^5),求一个含有k个整数的序列,要求以2为底,以序列内数字为幂的和为n,其中序列内最大的数最小,若有多个序列满足条件,输出 ...

  8. DSU on Tree浅谈

    DSU on tree 在之前的一次比赛中,学长向我们讲了了这样一个神奇的思想:DSU on tree(树上启发式合并),看上去就非常厉害--但实际上是非常暴力的一种做法;不过暴力只是看上去暴力,它在 ...

  9. ACDream手速赛2

    地址:http://acdream.info/onecontest/1014   都是来自Codeforce上简单题.   A. Boy or Girl 简单字符串处理   B. Walking in ...

随机推荐

  1. ORM版,学生信息管理单表查询..

    mysql 建学生表及课程表 添加内容 view.py from django.shortcuts import render,HttpResponse,redirect from . import ...

  2. JavaScript test//href

    目录 JavaScript test//href JavaScript test//href href 其实这个问题并不属于这里的.但是呢,由于一天晚上因为这个问题扰我"一夜不能眠" ...

  3. 解决"hibernate.hbm2ddl.auto" update值 无效

    <property name="schemaUpdate"> <value>true</value> </property> 若果是 ...

  4. HDU - 6096 :String (AC自动机,已知前后缀,匹配单词,弱数据)

    Bob has a dictionary with N words in it. Now there is a list of words in which the middle part of th ...

  5. 剑指offer-第四章解决面试题思路之总结

  6. RESTful 服务示例

    WCF服务轻量级服务,可供JS调用 返回值格式:XML.Json 工程结构: 示例代码: using System; using System.Collections.Generic; using S ...

  7. Django的CSRF机制

    原文链接:http://www.cnblogs.com/lins05/archive/2012/12/02/2797996.html 必须有的是: 1.每次初始化一个项目时,都能看到django.mi ...

  8. xlrd,xlwt读表格、写表格

    转:http://www.jb51.net/article/60510.htm

  9. Dawn 简单使用

     1. install npm install dawn -g 2. create project # 1. Create & Initialize $ dn init -t front # ...

  10. 致Oracle DBA 的一封信 (网上流传)

    1. 数据库的可用度,DBA 说了“不算”   --物化视图,加快查询速度 某些时候数据库的可用性,并不由DBA所设定.因为即使DBA对数据库有绝对掌控权,但用户可能从自己的工作和应用角度,与DBA的 ...