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/ ...
随机推荐
- mac关闭和开启启动声
1 关闭 sudo nvram SystemAudioVolume=%01 2 开启 sudo nvram -d SystemAudioVolume
- 安装RPM包或者安装源代码包
第十一章 安装RPM包或者安装源代码包 在windows下安装一个软件非常轻松,仅仅要双击.exe的文件,安装提示连续"下一步"就可以,然而linux系统下安装一个软件似乎并不那么 ...
- Data Url生成工具之HTML5 FileReader实现
百度经验版本号:怎样用HTML5的FileReader生成Data Url 上一篇讲了:用Visual Studio 2010编写Data Url生成工具C#版 今天用HTML5 FileReader ...
- bzoj2253
cdq分治+dp 看见三维偏序是cdq,互相包含是最长上升子序列 这个代码是错的 交了两份代码,发现手动出数据是不一样的... 不调了 #include<bits/stdc++.h> us ...
- Mariadb-lib
mariadb-libs-5.5.44-2.el7.centos.x86_64
- C# MySql 连接
1.将MySql.Data.dll引用到你的项目中 右键工程去完成. 2.using MySql.Data; using MySql.Data.MySqlClient; 3. MySqlConnec ...
- Kaka's Matrix Travels
http://poj.org/problem?id=3422 #include <stdio.h> #include <algorithm> #include <stri ...
- [Swift通天遁地]七、数据与安全-(11)如何检测应用程序中的内存泄露
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...
- 【转】 [MySQL 查询语句]——分组查询group by
group by (1) group by的含义:将查询结果按照1个或多个字段进行分组,字段值相同的为一组(2) group by可用于单个字段分组,也可用于多个字段分组 select * from ...
- go 学习成长之路
一.go的搭建 二.初识go 三.混个脸熟--go 四.go的语言结构 五.go的常量与变量 六.go基础数据类型 七.go 条件语句 八.go 运算符 九.go条件语句switch 十.go循环语句 ...