您的位置:首页 > 其它

九度 1337 寻找最长合法括号序列

2011-12-09 10:58 169 查看
http://ac.jobdu.com/problem.php?id=1337

在混乱中写完了最后一个循环,要时刻警惕越界。。 还有就是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);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: