题目链接: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 ;
}

最新文章

  1. C#移动跨平台开发(2)Xamarin移动跨平台解决方案是如何工作的?
  2. cx_Oracle摘记
  3. 通过html和css做出下拉导航栏的效果
  4. testng教程之testng.xml的配置和使用,以及参数传递
  5. 小知识:Python函数传递变长
  6. C#手工注入辅助工具
  7. mysql 主主复制的配置流程
  8. zkclient
  9. 安装完Oracle数据库,给scott账户解锁方法
  10. Project_2007关键
  11. OPENCV条形码检测与识别
  12. Eslint配置
  13. XXXX is not in the sudoers file. This incident will be reported解决方法
  14. centos7.6编译安装php7.2.11及redis/memcached/rabbitmq/openssl/curl等常见扩展
  15. [Java] [Singleton] [DCL][happens-before]
  16. [ Talk is Cheap Show me the CODE ] : jQuery Mobile页面布局
  17. 设计模式のFlyweight(享元模式)----结构模式
  18. Flask主要知识点
  19. 『Python CoolBook:heapq』数据结构和算法_heapq堆队列算法&amp;容器排序
  20. 小学四则运算APP 第一阶段冲刺

热门文章

  1. Hdu 1429 胜利大逃亡(续) 分类: Brush Mode 2014-08-07 17:01 92人阅读 评论(0) 收藏
  2. 管理员必须掌握的八个cmd命令
  3. DevExpress12.2.4 GridControl相关技巧
  4. CSLight研究院之学习笔记结合NGUI(一)
  5. ASP.NET MVC的处理管线
  6. 全自动化的 Android 编译管线
  7. iOS字符串大小转换
  8. ios下划线变量:为什么变量前要加下划线才有用?
  9. 妙味课堂——HTML+CSS(第四课)(一)
  10. linux下tigervnc-servere服务的安装与使用