hihocoder 1300 展胜地的鲤鱼旗 dp
2016-05-07 09:22
204 查看
考虑动态规划 F[i] 代表以第 i 个字符为结尾的平衡串个数, 预处理出每个与右括号匹配的左括号 j,如果存在的话, 那么 f[i]=f[j-1]+1; 复杂度也是 O(n)。
View Code
#include <stdio.h> #include <string.h> #include <math.h> #include <stack> #include <algorithm> using namespace std; typedef long long LL; const int N=1e6+5; const int INF=0x3f3f3f3f; int match ,n; char s ; stack<int>st; LL dp [2]; int main(){ scanf("%s",s+1); n=strlen(s+1); LL ans=0; for(int i=1;i<=n;++i){ if(s[i]=='(')st.push(i); else { if(!st.empty()){ match[i]=st.top(); match[st.top()]=i; st.pop(); } } } for(int i=1;i<=n;++i){ dp[i][0]=dp[i-1][1]+dp[i-1][0]; if(match[i]>i||!match[i])continue; dp[i][1]=dp[match[i]-1][1]+1; } printf("%lld\n",dp [0]+dp [1]); return 0; }
View Code
相关文章推荐
- day5.03总结_友录_优化(异步和缓存和回调)
- Unity中Awake与Start函数的区别
- 通过apperance设置的属性,必须得view加载完毕之后才能生效,我们可以强制刷新
- 设置导航栏的字体大小
- 判断两个窗口是否有交集
- day4.29总结_友录_设置圆圈和显示联系人的详细信息
- 交换机VLAN的建立和配置 ( 思科模拟仿真6.0实现)
- 点击图片放大
- 将文字保存到粘贴板
- 把状态栏交给application管理1
- JS高级前端开发群加群说明
- 设计模式之桥接模式
- Oracle 11g Data Guard 之角色转换
- day4.28总结_友录_启动页面和联系人页面
- web前端开发分享-css,js 不错的书籍
- [算法]如何实现栈
- xib取消cell选中状态
- day4.27总结_Intent和Application
- norflash芯片内执行(XIP)
- 写一个自己的数据库帮助类