您的位置:首页 > 其它

hihocoder 1300 展胜地的鲤鱼旗 dp

2016-05-07 09:22 204 查看
考虑动态规划 F[i] 代表以第 i 个字符为结尾的平衡串个数, 预处理出每个与右括号匹配的左括号 j,如果存在的话, 那么 f[i]=f[j-1]+1; 复杂度也是 O(n)。

#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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: