51nod 1478 括号序列的最长合法子段(栈-括号匹配寻找最长合法子串长度及其个数)
2017-03-16 01:28
337 查看
1478 括号序列的最长合法子段
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
这里有另一个关于处理合法的括号序列的问题。
如果插入“+”和“1”到一个括号序列,我们能得到一个正确的数学表达式,我们就认为这个括号序列是合法的。例如,序列"(())()", "()"和"(()(()))"是合法的,但是")(", "(()"和"(()))("是不合法的。
这里有一个只包含“(”和“)”的字符串,你需要去找到最长的合法括号子段,同时你要找到拥有最长长度的子段串的个数。
Input
Output
Input示例
Output示例
题解:看多题目了,以为是找所有合法子串个数。想了半天,发现时找最长的。 那么就很简单了,用一个栈维护,将没办法形成匹配的括号及其位置放进一个栈里面,那么最后栈内每两个括号之间就有一个合法串,然后从前往后扫一遍,记录下最长合法串长度及其个数就好了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 1e6+10;
struct node
{
int id;
char s;
}temp;
stack<node> tac;
char str[maxn];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
int Max=0,cnt=1;
while(!tac.empty())
tac.pop();
for(int i=0;i<len;++i)
{
if(str[i]=='(')
{
temp.id=i;
temp.s='(';
tac.push(temp);
}
else
{
if(tac.empty())
{
temp.id=i;
temp.s=')';
tac.push(temp);
}
else
{
temp=tac.top();
if(temp.s==')')
{
temp.id=i;
tac.push(temp);
}
else
tac.pop();
}
}
}
if(tac.empty())
Max=len;
else
{
temp=tac.top();
int L=len-temp.id-1;
Max=L;
while(!tac.empty())
{
temp=tac.top();
tac.pop();
if(tac.empty()){
if(temp.id>Max)
{
Max=temp.id;
cnt=1;
}
else if(temp.id==Max)
cnt++;
}
else
{
node step=tac.top();
L=temp.id-step.id-1;
if(L==Max)
cnt++;
else if(L>Max){
cnt=1;
Max=L;
}
}
}
}
if(Max==0)
cnt=1;
printf("%d %d\n",Max,cnt);
}
return 0;
}
题目来源: CodeForces
基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题
收藏
关注
这里有另一个关于处理合法的括号序列的问题。
如果插入“+”和“1”到一个括号序列,我们能得到一个正确的数学表达式,我们就认为这个括号序列是合法的。例如,序列"(())()", "()"和"(()(()))"是合法的,但是")(", "(()"和"(()))("是不合法的。
这里有一个只包含“(”和“)”的字符串,你需要去找到最长的合法括号子段,同时你要找到拥有最长长度的子段串的个数。
Input
第一行是一个只包含“(”和“)”的非空的字符串。它的长度不超过 1000000。
Output
输出合格的括号序列的最长子串的长度和最长子串的个数。如果没有这样的子串,只需要输出一行“0 1”。
Input示例
)((())))(()())
Output示例
6 2
题解:看多题目了,以为是找所有合法子串个数。想了半天,发现时找最长的。 那么就很简单了,用一个栈维护,将没办法形成匹配的括号及其位置放进一个栈里面,那么最后栈内每两个括号之间就有一个合法串,然后从前往后扫一遍,记录下最长合法串长度及其个数就好了。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <stack>
using namespace std;
const int maxn = 1e6+10;
struct node
{
int id;
char s;
}temp;
stack<node> tac;
char str[maxn];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
int Max=0,cnt=1;
while(!tac.empty())
tac.pop();
for(int i=0;i<len;++i)
{
if(str[i]=='(')
{
temp.id=i;
temp.s='(';
tac.push(temp);
}
else
{
if(tac.empty())
{
temp.id=i;
temp.s=')';
tac.push(temp);
}
else
{
temp=tac.top();
if(temp.s==')')
{
temp.id=i;
tac.push(temp);
}
else
tac.pop();
}
}
}
if(tac.empty())
Max=len;
else
{
temp=tac.top();
int L=len-temp.id-1;
Max=L;
while(!tac.empty())
{
temp=tac.top();
tac.pop();
if(tac.empty()){
if(temp.id>Max)
{
Max=temp.id;
cnt=1;
}
else if(temp.id==Max)
cnt++;
}
else
{
node step=tac.top();
L=temp.id-step.id-1;
if(L==Max)
cnt++;
else if(L>Max){
cnt=1;
Max=L;
}
}
}
}
if(Max==0)
cnt=1;
printf("%d %d\n",Max,cnt);
}
return 0;
}
相关文章推荐
- 字符串空格替换、合法括号序列判断、最长无重复子串长度
- 九度OJ 1337:寻找最长合法括号序列 (DP)
- 九度OJ 1342:寻找最长合法括号序列II (DP)
- Longest Valid Parentheses (求最长有效匹配括号子串的长度)
- 九度OJ 1342:寻找最长合法括号序列II (DP)
- 九度笔记之 1337:寻找最长合法括号序列
- 九度 题目1342:寻找最长合法括号序列II(25分)
- 九度笔记之 1342:寻找最长合法括号序列II(25分)-给括号当红娘,解决最长合法字符串问题
- Longest Valid Parentheses (求最长有效匹配括号子串的长度)
- 【九度】题目1342:寻找最长合法括号序列II(25分)
- Longest Valid Parentheses (求最长有效匹配括号子串的长度)
- 九度 题目1337:寻找最长合法括号序列
- 寻找最长合法括号序列九度0J
- 九度 1337 寻找最长合法括号序列
- [Jobdu] 题目1337:寻找最长合法括号序列
- 九度 1342 寻找最长合法括号序列II
- 求最长有效匹配括号子串的长度(Longest Valid Parentheses)
- 【九度】题目1337:寻找最长合法括号序列
- LeetCode: Longest Valid Parentheses (求最长有效匹配括号子串的长度)
- LongestValidParentheses, 求最长合法括号子串长度-----同类问题ValidParentheses,GenerateParentheses