传送门:http://codeforces.com/contest/918/problem/C

一个括弧串由字符‘(’和‘)’组成。一个正确的串可被递归地定义,定义如下:

①空串e是一个正确的串;

②若串s是一个正确的串,则串(s)也是一个正确的串;

③若串st均是正确的串,则串st也是一个正确的串。

对于一个由字符‘(’、‘)’和‘?’组成的串s,考虑其子串s.substr(i,j):将这个子串的符号‘?’置换为‘(’或‘)’,若置换后的串是一个正确的串,则原来的子串是一个完美的串。试统计串s中所有的非空完美子串的数目。

这个问题的简单化版本就是括弧匹配,可以通过模拟“栈”以实现:

bool is_correct(const char* ch)
{
int top = ; //top of stack
for (int i = ; ch[i] != '\0'; i++) {
if (ch[i] == '(') top++; //open parameter
else top--; //closed parameter
if (top < ) return false;
}
if (top == ) return true;
return false;
}

接下来,引入符号‘?’,则可以计算top值的上确界sup和下确界inf。将‘?’置换成‘(’,以计算上界;置换成‘)’,以计算下界:

bool is_correct(const char* ch)
{
int sup = , inf = ;
for (int i = ; ch[i] != '\0'; i++) {
if (ch[i] == '(') sup++, inf++; //open parameter
else if (ch[i] == ')') sup--, inf--; //closed parameter
else sup++, inf--;
if (sup < ) return false;
if (inf < ) inf = ;
}
if (strlen(ch) % == )
if (inf == ) return true;
return false;
}

现在考虑本题的问题,则可枚举所有的有序对<i,j>,时间复杂度为O(n2)。参考程序如下:

#include <stdio.h>
#define MAX_L 10000 char ch[MAX_L]; int main(void)
{
scanf("%s", ch);
int ans = ;
for (int i = ; ch[i] != '\0'; i++) {
int sup = , inf = ;
for (int j = i; ch[j] != '\0'; j++) {
if (ch[j] == '(') sup++, inf++;
else if (ch[j] == ')') sup--, inf--;
else sup++, inf--;
if (sup < ) break;
if (inf < ) inf = ;
if (((j - i) % ))
if (inf == ) ans++;
}
}
printf("%d\n", ans);
return ;
}

Codeforces 918C/917A - The Monster的更多相关文章

  1. Codeforces 918C - The Monster

    918C - The Monster 思路1: 右键在新窗口打开图片 代码: #include<bits/stdc++.h> using namespace std; #define ll ...

  2. Codeforces 918C The Monster(括号匹配+思维)

    题目链接:http://codeforces.com/contest/918/problem/C 题目大意:给你一串字符串,其中有'('.')'.'?'三种字符'?'可以当成'('或者')'来用,问该 ...

  3. CodeForces 917A The Monster 贪心+思维

    As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Chris ...

  4. 【codeforces 787A】The Monster

    [题目链接]:http://codeforces.com/contest/787/problem/A [题意] 把b一直加a->得到x 把d一直加c->得到y 然后问你x和y可不可能有相同 ...

  5. Codeforces 488C Fight the Monster

    Fight the Monster time limit per test             1 second                                   memory ...

  6. codeforces 487a//Fight the Monster// Codeforces Round #278(Div. 1)

    题意:打怪兽.可增加自己的属性,怎样在能打倒怪兽的情况下花费最少? 这题关键要找好二分的量.一开始我觉得,只要攻击到101,防御到100,就能必胜,于是我对自己的三个属性的和二分(0到201),内部三 ...

  7. CF 917A The Monster 【括号匹配】

    [链接]:CF Examples inputCopy ((?)) outputCopy 4 inputCopy ??()?? outputCopy 7 说明 For the first sample ...

  8. The Monster CodeForces - 917A (括号匹配)

    链接 大意:给定字符串, 只含'(',')','?', 其中'?'可以替换为'('或')', 求有多少个子串可以的括号可以匹配 (不同子串之间独立) 记$s_($为'('个数, $s_)$为')'个数 ...

  9. Codeforces Round #278 (Div. 1) A. Fight the Monster 暴力

    A. Fight the Monster Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/487/ ...

随机推荐

  1. 【bzoj1082】栅栏[SCOI2005]

    显然我们取的肯定是前ans块木板.然后砍的木材也应该是从小到大砍(如果小的木材可以满足条件,就一定不会去动大的木材) 所以两遍排序. 二分答案. 然后对于要取的每块木板,我们搜索它是在第x块木板上砍下 ...

  2. DIV+CSS在不同浏览器中的表现

     在给员工培训DIV+CSS的过程中.他们向我提出了非常多问题,有些问题我自己也没有想到过于是抽了些时间自己进行了一番实验,所有实验在IE7和Firefox中进行: 实验一.假设一个div没有指定 ...

  3. android kl 文件的作用【转】

    本文转载自:http://blog.csdn.net/u013308744/article/details/49274069 首先看touchscreen的kl文件 # Copyright (c) 2 ...

  4. SQL 字符串处理函数大全

    select语句中只能使用sql函数对字段进行操作(链接sql server),select 字段1 from 表1 where 字段1.IndexOf("云")=1;这条语句不对 ...

  5. 【Hnoi2013】Bzoj3143 游走

    Position: http://www.lydsy.com/JudgeOnline/problem.php?id=3143 List Bzoj3143 Hnoi2013 游走 List Descri ...

  6. js和php中几种生成验证码的方式

    之前做过取随机数和生成验证码的练习,都是通过取随机数作为数组下标,然后从数组中取值的方式(js): /*验证码*/ function sj_yzm(){ //存一个包括数字和字母的数组 var zon ...

  7. Unity - 简单实例化的应用

    项目描述:每帧实例化一个随机颜色的物体(Cube),坐标在某范围内随机:且物体每帧都会缩小,当缩小到一定的尺寸时,就销毁物体 代码描述: public class CubeSpawner : Mono ...

  8. 如何解决error LNK2001(转载)

    转自:http://www.cnblogs.com/myzhijie/articles/1658545.html 解决外部符号错误:_main,_WinMain@16,__beginthreadex ...

  9. 题解报告:hdu 1527 取石子游戏(威佐夫博弈)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1527 Problem Description 有两堆石子,数量任意,可以不同.游戏开始由两个人轮流取石 ...

  10. 3CSS基本语法

    ------------------------- --------------------------------------- -------------------------------- & ...