九度 1337 寻找最长合法括号序列
2011-12-09 10:58
169 查看
http://ac.jobdu.com/problem.php?id=1337
在混乱中写完了最后一个循环,要时刻警惕越界。。 还有就是indent还是不会用,直接把代码缩进搞乱了
在混乱中写完了最后一个循环,要时刻警惕越界。。 还有就是indent还是不会用,直接把代码缩进搞乱了
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <stack> using namespace std; int N; char str[1000002]; bool flags[1000002] = { false }; stack < int >S; bool match(char a,char b) { if(a=='('&&b==')') return true; return false; } int main () { while (scanf ("%d", &N) != EOF) { scanf ("%s", str); while (!S.empty ()) S.pop (); memset (flags, false, sizeof (flags)); S.push (0); int i, j; int len = strlen (str) - 1; for (i = 1; i <= len; i++) { if (S.empty ()) S.push (i); else if (match (str[S.top ()], str[i])) { flags[i] = true; flags[S.top ()] = true; S.pop (); } else S.push (i); } int max_len = -1; int num = 0; i = j = 0; while (j <= len) { while (j<=len&&!flags[j]) j++; i = j; while (j<=len&&flags[j]) j++; if (j - i > max_len) { max_len = j - i ; num = 1; } else if (j - i == max_len) { num++; } i = j + 1; } printf("%d %d\n",max_len,num); } }
相关文章推荐
- 九度 题目1337:寻找最长合法括号序列
- 【九度】题目1337:寻找最长合法括号序列
- 九度笔记之 1337:寻找最长合法括号序列
- 九度OJ 1342:寻找最长合法括号序列II (DP)
- 九度OJ 1342:寻找最长合法括号序列II (DP)
- 九度 1342 寻找最长合法括号序列II
- 【九度】题目1342:寻找最长合法括号序列II(25分)
- 九度 题目1342:寻找最长合法括号序列II(25分)
- [Jobdu] 题目1337:寻找最长合法括号序列
- 九度笔记之 1342:寻找最长合法括号序列II(25分)-给括号当红娘,解决最长合法字符串问题
- 寻找最长合法括号序列九度0J
- 九度OJ 1337:寻找最长合法括号序列 (DP)
- 九度OJ 1337:寻找最长合法括号序列 (DP)
- 九度题目1342:寻找最长合法括号序列II
- 51nod 1478 括号序列的最长合法子段(栈-括号匹配寻找最长合法子串长度及其个数)
- 最长合法括号序列
- 字符串空格替换、合法括号序列判断、最长无重复子串长度
- leetcode 522. Longest Uncommon Subsequence II 最长非公共子序列 + 暴力寻找子串
- 51nod 1478-括号序列的最长合法子段(模拟)
- 矩形网格中寻找最长递增序列