Codeforces 918C/917A - The Monster
传送门:http://codeforces.com/contest/918/problem/C
一个括弧串由字符‘(’和‘)’组成。一个正确的串可被递归地定义,定义如下:
①空串e是一个正确的串;
②若串s是一个正确的串,则串(s)也是一个正确的串;
③若串s、t均是正确的串,则串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的更多相关文章
- Codeforces 918C - The Monster
918C - The Monster 思路1: 右键在新窗口打开图片 代码: #include<bits/stdc++.h> using namespace std; #define ll ...
- Codeforces 918C The Monster(括号匹配+思维)
题目链接:http://codeforces.com/contest/918/problem/C 题目大意:给你一串字符串,其中有'('.')'.'?'三种字符'?'可以当成'('或者')'来用,问该 ...
- CodeForces 917A The Monster 贪心+思维
As Will is stuck in the Upside Down, he can still communicate with his mom, Joyce, through the Chris ...
- 【codeforces 787A】The Monster
[题目链接]:http://codeforces.com/contest/787/problem/A [题意] 把b一直加a->得到x 把d一直加c->得到y 然后问你x和y可不可能有相同 ...
- Codeforces 488C Fight the Monster
Fight the Monster time limit per test 1 second memory ...
- codeforces 487a//Fight the Monster// Codeforces Round #278(Div. 1)
题意:打怪兽.可增加自己的属性,怎样在能打倒怪兽的情况下花费最少? 这题关键要找好二分的量.一开始我觉得,只要攻击到101,防御到100,就能必胜,于是我对自己的三个属性的和二分(0到201),内部三 ...
- CF 917A The Monster 【括号匹配】
[链接]:CF Examples inputCopy ((?)) outputCopy 4 inputCopy ??()?? outputCopy 7 说明 For the first sample ...
- The Monster CodeForces - 917A (括号匹配)
链接 大意:给定字符串, 只含'(',')','?', 其中'?'可以替换为'('或')', 求有多少个子串可以的括号可以匹配 (不同子串之间独立) 记$s_($为'('个数, $s_)$为')'个数 ...
- 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/ ...
随机推荐
- 码位(code position/point)Unicode 编码与 Python 2/3 编码兼容性问题
Unicode HOWTO 0. 码位(code position/point) 一个码位由某个数值表示,全部码位共同构成其码值空间(code space). ASCII,0~7Fhex(128) 拓 ...
- vs打开wixproj后缀文件
1.在正常情况下vs是无法打开wixproj工程的,能打开也只能是以记事本方式打开该文件本身 2.所以此时需要下载wixtool,安装后即可打开上述类型文件 3.最好也安装好vs对应版本的扩展包 4. ...
- 【WIP】客户端JavaScript DOM
创建: 2017/10/12 初步完成: 2017/10/15 更新: 2017/10/14 标题加上[WIP],继续完成 [TODO] 补充暂略的, 搜[略] DOM树 概要 基本 ...
- P4280 [AHOI2008]逆序对
传送门 有一个不会证明的贪心:从左到右考虑每一个位置,然后在每一个位置都贪心选取能让该位置构成的逆序对最少的数.判断逆序对的话只要记一下前缀小于等于某数的总数和后缀小于等于某数的总数就行了 //min ...
- 微信小程序压缩图片并上传到服务器(拿去即用)
这里注意一下,图片压缩后的宽度是画布宽度的一半 canvasToTempFilePath 创建画布的时候会有一定的时间延迟容易失败,这里加setTimeout来缓冲一下 这是单张图片压缩,多张的压缩暂 ...
- GIT分支的一些开发心得
从本地的master分支创建新的分支 $ git checkout -b dev Switched to a new branch 'dev' 上面那条命令可以分为两步 $ git branch de ...
- HttpFileCollection 类使用
public ActionResult GetForm() { HttpRequest request = System.Web.HttpContext.Curre ...
- Android 关于android.os.Build介绍
关于Build类的介绍 这个类为一个获取设备一些初始化信息的类,该类的主要信息都是通过一些static的字段获得: public static final String BOARD The name ...
- python网络爬虫数据中的三种数据解析方式
一.正则解析 常用正则表达式回顾: 单字符: . : 除换行以外所有字符 [] :[aoe] [a-w] 匹配集合中任意一个字符 \d :数字 [0-9] \D : 非数字 \w :数字.字母.下划线 ...
- [转]sed常用命令总结
转自:http://blog.chinaunix.net/uid-26963748-id-3249732.html 一.Sed简介 Sed:Stream Editor 流式编辑器 又称行编辑器,每次 ...