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/ ...
随机推荐
- ios6--UILabel
// // ViewController.m // 02-UILabel的使用 // // UILabel显示一段文字. #import "ViewController.h" @i ...
- Window attributes属性详解
以下属性以Dialog为例来讲解: <item name="windowBackground"> 窗体的背景 </item><item name=&q ...
- JSP-Runoob:JSP 表达式语言
ylbtech-JSP-Runoob:JSP 表达式语言 1.返回顶部 1. JSP 表达式语言 JSP表达式语言(EL)使得访问存储在JavaBean中的数据变得非常简单.JSP EL既可以用来创建 ...
- 61.员工信息管理Extjs 页面
1.员工信息管理jsp <%@ page language="java" pageEncoding="UTF-8"%> <script typ ...
- 数据库异常 :java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)
最近在新项目中突然出现了 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES) ...
- Mobile
模块===包 传统开发:整个网页我们写了一个js文件,所有的特效都写在里面了. 缺点:耦合度太高,代码关联性太强,不便于后期维护,会造成全局污染. 发生的请求次数过多,依赖模糊,难于维护. 以上都 ...
- 在chrome里模拟调试微信浏览器
开发者模式(下面有配图): 开发者模式/DevTools.More tools/Network conditions/User agent/ Custom/安卓或ios代理配置配置 更改User ag ...
- 如何获取select控件的option值和Value?
案例: <select id="paId" class="text3"> <option value=& ...
- sql数据库中常用连接
很简单的知识点,今天有点搞不清楚左外连接,右外连接:详见以下: --表stu id name 1, Jack 2, Tom 3, Kity 4, nono --表exam id grade 1, 56 ...
- firefox 附加组件栏安装
firefox 在升级到 30的版本后,发现附加组件栏不兼容了. 搜索组件,add-on bar 会得到一个 new add-on bar的组件,安装完后发现上面不显示ip, 后来才发现,应该安装Th ...