题意

一串括号字符串,里面存在一些‘?’,其中‘?’既可以当作 '(' 又可以当作 ')' ,计算有多少对(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. C++11新特性之右值引用(&&)、移动语义(move)、完美转换(forward)

    1. 右值引用 个人认为右值引用的目的主要是为了是减少内存拷贝,优化性能. 比如下面的代码: String Fun() { String str = "hello world"; ...

  2. POJ3764,BZOJ1954 The xor-longest Path

    题意 In an edge-weighted tree, the xor-length of a path p is defined as the xor sum of the weights of ...

  3. 【CSS3】 - 初识CSS3

    .navdemo{ width:560px; height: 50px; font:bold 0/50px Arial; text-align:center; margin:40px auto 0; ...

  4. spark-shell与scala生成parquet文件

    spark-shell用户 spark-shell只能spark用户来进行初始化,如果用hdfs或者root将会卡在spark-shell的初始化那个地方.可以用这两个用户来验证spark安装时成功地 ...

  5. MySQL插入中文时出现ERROR 1406 (22001): Data too long for column 'name' at row 1 (转)

    使用命令行方式登陆到MySQL服务器, 建立一个数据库,数据库编码设为UTF-8.此时,如果直接在命令行窗口使用insert语句插入中文,就遇到类似 ERROR 1406 (22001): Data ...

  6. spring mvc加了@produces注解后报406

    问题背景:调用http的post接口返回一个String类型的字符串时中文出现乱码,定位出问题后在@RequestMapping里加produces注解produces = "applica ...

  7. 监控文件的watchdog

    watchdog用来监控指定目录/文件的变化,如添加删除文件或目录.修改文件内容.重命名文件或目录等,每种变化都会产生一个事件,且有一个特定的事件类与之对应,然后再通过事件处理类来处理对应的事件,怎么 ...

  8. linux 下安装php扩展

    linux下安装php扩展 步骤: 1.在扩展解压包目录执行 phpize 2.执行 ./configure --with-php-config=/usr/local/php/bin/php-conf ...

  9. winfrom绘制渐变 / 调用浏览器访问指定地址

    private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) {//绘制渐变色背景 Graphics g ...

  10. 011. Python中*args, **kwargs 和 pass 和self 解释

    *args, **kwargs →在python都表示可变参数, *args表示任意多个任意类型无名参数, 是一个元组; **kwargs表示关键字参数(key/value参数), 是一个字典,接收的 ...