[HIHO1300]展胜地的鲤鱼旗(栈,dp)
2024-08-27 11:25:11
题目链接:http://hihocoder.com/problemset/problem/1300
给一个字符串,只包含'('和')',问存在多少个子串似的括号是匹配的。
匹配规则在题干中描(蒻)述(语)得(文)非常烂!
维护一个栈,栈中保存'('的下标。当遇到')'并且栈非空的时候,说明已经匹配到一对括号。取出栈顶'('的位置idx。并且记录此时状态,dp(i)=dp(idx-1)+1。
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath> using namespace std; //kirai maxn
const int maxn = ;
int n;
int st[maxn], top;
int dp[maxn];
char str[maxn]; int main() {
// freopen("in", "r", stdin);
while(~scanf("%s", str)) {
top = ;
n = strlen(str);
memset(dp, , sizeof(dp));
for(int i = ; i < n; i++) {
if(str[i] == '(') st[top++] = i;
else {
if(top > ) {
int idx = st[--top];
dp[i] = + dp[idx-];
}
}
}
int ans = ;
for(int i = ; i < n; i++) {
printf("%d ", dp[i]);
ans += dp[i];
}
printf("%d\n", ans);
}
return ;
}
最新文章
- C#移动跨平台开发(2)Xamarin移动跨平台解决方案是如何工作的?
- cx_Oracle摘记
- 通过html和css做出下拉导航栏的效果
- testng教程之testng.xml的配置和使用,以及参数传递
- 小知识:Python函数传递变长
- C#手工注入辅助工具
- mysql 主主复制的配置流程
- zkclient
- 安装完Oracle数据库,给scott账户解锁方法
- Project_2007关键
- OPENCV条形码检测与识别
- Eslint配置
- XXXX is not in the sudoers file. This incident will be reported解决方法
- centos7.6编译安装php7.2.11及redis/memcached/rabbitmq/openssl/curl等常见扩展
- [Java] [Singleton] [DCL][happens-before]
- [ Talk is Cheap Show me the CODE ] : jQuery Mobile页面布局
- 设计模式のFlyweight(享元模式)----结构模式
- Flask主要知识点
- 『Python CoolBook:heapq』数据结构和算法_heapq堆队列算法&;容器排序
- 小学四则运算APP 第一阶段冲刺
热门文章
- Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
- 管理员必须掌握的八个cmd命令
- DevExpress12.2.4 GridControl相关技巧
- CSLight研究院之学习笔记结合NGUI(一)
- ASP.NET MVC的处理管线
- 全自动化的 Android 编译管线
- iOS字符串大小转换
- ios下划线变量:为什么变量前要加下划线才有用?
- 妙味课堂——HTML+CSS(第四课)(一)
- linux下tigervnc-servere服务的安装与使用