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/ ...
随机推荐
- bzoj 1022 小约翰的游戏John
题目大意: n堆石子,两个人轮流取石子,每个人取的时候,可以随意选择一堆石子 在这堆石子中取走任意多的石子,但不能一粒石子也不取,取到最后一粒石子的人算输 思路: 首先当每堆石子数都为1时,偶数为先手 ...
- json Date对象在js中的处理办法
我们在程序用往往通过ajax方式返回json数据,json中包含Date对象时,在js中是Object对象.可以方式获取: 1.new Date(yourJsonDate.time); //你用你的返 ...
- codevs1369 xth 砍树(线段树)
1369 xth 砍树 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 在一个凉爽的夏夜,xth 和 rabbi ...
- 解决:xxx is not in the sudoers file.This incident will be reported.的解决方法
Linux中普通用户用sudo执行命令时报”xxx is not in the sudoers file.This incident will be reported”错误,解决方法就是在/etc/s ...
- 如何解决error LNK2001(转载)
转自:http://www.cnblogs.com/myzhijie/articles/1658545.html 解决外部符号错误:_main,_WinMain@16,__beginthreadex ...
- ACM_二维数组的查找
二维数组的查找 Time Limit: 2000/1000ms (Java/Others) Problem Description: 给定一个n*m的二维数组,保证a[i][j] < a[i+1 ...
- 316 Remove Duplicate Letters 去除重复字母
给定一个仅包含小写字母的字符串,去除重复的字母使得所有字母出现且仅出现一次.你必须保证返回结果是所有可能结果中的以字典排序的最短结果.例如:给定 "bcabc"返回 "a ...
- webSocket客服在线交谈
一>用户端 <%@ page language="java" pageEncoding="UTF-8" %><%@ taglib uri ...
- 【译】x86程序员手册15-5.2页转换
5.2 Page Translation 页转换 In the second phase of address transformation, the 80386 transforms a linea ...
- 11、scala函数式编程
1.将函数赋值给变量 2.匿名函数 3.高阶函数 4.高阶函数的类型推断 5.Scala的常用高级函数 6.闭包 7.SAM转换 8.Currying函数 9.return 1.将函数赋值给变量 Sc ...