FZU Problem 2030 括号问题
2014-03-14 17:21
253 查看
/* 对于小数据用这dfs,大数据就用递推的思想。 */ #include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <queue> #include <map> #include <stack> #include <vector> using namespace std ; const int maxn = 20; char str[maxn]; int num[maxn]; bool judge(char *st){ int top = 0; int l = strlen(st); for(int i = 0;i < l;i++){ if(st[i] == '(') top++; else if(top) top--; else return 0; } if(top) return 0; return 1; } int len,ans; void dfs(int x){ if(x == len){ if(judge(str)) ans++; } else{ str[num[x]] = '('; dfs(x+1); str[num[x]] = ')'; dfs(x+1); } } int main(){ //freopen("test.txt","r",stdin); //freopen("out.txt","w",stdout); while(~scanf("%s",str)){ len = 0; int l = strlen(str); for(int i = 0;i < l;i++){ if(str[i] == '?') num[len++] = i; } ans = 0; dfs(0); printf("%d\n",ans); } return 0; } /* 对于数据比较小的,直接用dfs即可,数据要是比较大的话,dfs肯定TLE,所以这时应该采取递推的思想。 */ #include <stdio.h> #include <string.h> #include <iostream> #include <algorithm> using namespace std; const int maxn = 1005; char str[maxn]; int dp[maxn][maxn]; int main(){ while(~scanf("%s",str)){ int len = (int)strlen(str); dp[0][1] = 1; for(int i = 1;i < len;i++){ for(int j = 0;j < len;j++){ if(str[i] == '(') dp[i][j] = dp[i-1][j-1]; else if(str[i] == ')') dp[i][j] = dp[i-1][j+1]; else dp[i][j] = dp[i-1][j-1] + dp[i-1][j+1]; } } /*for(int i = 0;i < len;i++){ for(int j =0;j < len;j++) printf("%d ",dp[i][j]); printf("\n"); }*/ printf("%d\n",dp[len - 1][0]); } return 0; }
相关文章推荐
- FZU 2030 括号问题 (括号匹配)
- FZU---2030 括号问题[爆搜||DP]
- fzu 2030 括号问题(DFS)
- FZU 2030 括号问题 dfs/dp
- FZU 2030 括号问题
- FZU 2030 括号问题 (DFS)
- dp:FZU2030括号问题
- FZU 2030 括号问题
- FZU FOJ 2030 括号问题【dp】||【暴搜+栈判断括号匹配】
- FZU Problem 1230 区间相交问题 &&XTU 1151 bus
- http://acm.fzu.edu.cn/problem.php?pid=1564 C(n,k)和素数P的问题
- Knapsack problem (FZU_2214,福建省第六届ACM-problemC) 01背包+初始化问题+渐缩问题
- 括号配对问题http://acm.nyist.net/JudgeOnline/problem.php?pid=2
- Knapsack problem (FZU_2214,福建省第六届ACM-problemC) 01背包+初始化问题+渐缩问题
- Problem 1582 众数问题 from http://acm.fzu.edu.cn/problem.php?pid=1582
- Problem 1207 半数集问题 from http://acm.fzu.edu.cn/problem.php?pid=1207
- FZU 2218 Simple String Problem (状压DP解决集合不相同元素问题)
- FZU2030(括号匹配)
- Problem 1202 信与信封问题 from http://acm.fzu.edu.cn/problem.php?pid=1202