一题DP题
输入一串由'(',')','[',']'组成的字符串,对于[ ],( ),[ ( ) ], ( [ ] ),都是合法的匹配。而对于( ],[ )等则是不合法的匹配,你的任务是找出括号集中最长合法匹配
我们先处理只有'('和')'的情况,遇到一个')',如果前面是一个'(',那么长度是2的"()"肯定是可以的了,但是如果'('前面已经有匹配了的,如前面如果是一个已经匹配了的"()",那么我们需要把它也加进去.
如上,我们再把'['和']'加进来
/**/
/*
0.13s
DP
*/
#include
"
stdio.h
"
#include
<
string
.h
>
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
char
str[
100002
] ;
int
DP[
100002
] ;
bool
mark[
100002
] ;
![](https://i-blog.csdnimg.cn/blog_migrate/6810355c2f78c12e91b7997a8e8c583a.gif)
int
main (
void
)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
int i, j ;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
while ( 1 == scanf ( "%s", str ) ) ...{
int start = 0, end = -1, len = -1 ;
DP[0] = -1 ; mark[0] = false ;
int pos = strlen(str) ;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for ( i = 1 ; i < pos ; i++ ) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( str[i] == '(' || str[i] == '[' ) ...{
mark[i] = false ;
//DP[i] = -1 ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else if ( str[i] == ')' ) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( str[i-1] == '(' ) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( i-2 >= 0 && mark[i-2] ) ...{
mark[i] = true ;
DP[i] = DP[i-2] ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else ...{
mark[i] = true ;
DP[i] = i-1 ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else if ( mark[i-1] && DP[i-1] != 0 ) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( str[DP[i-1]-1] == '(' ) ...{
mark[i] = true ;
if ( DP[i-1]-2 >= 0 && mark[DP[i-1]-2] )
DP[i] = DP[DP[i-1]-2] ;
else
DP[i] = DP[i-1]-1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}
else mark[i] = false ;
}
else mark[i] = false ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else if ( str[i] == ']' ) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( str[i-1] == '[' ) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( i-2 >= 0 && mark[i-2] ) ...{
mark[i] = true ;
DP[i] = DP[i-2] ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else ...{
mark[i] = true ;
DP[i] = i-1 ;
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
else if ( mark[i-1] && DP[i-1] != 0 ) ...{
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( str[DP[i-1]-1] == '[' ) ...{
mark[i] = true ;
if ( DP[i-1]-2 >= 0 && mark[DP[i-1]-2] )
DP[i] = DP[DP[i-1]-2] ;
else
DP[i] = DP[i-1]-1 ;
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}
else mark[i] = false ;
}
else mark[i] = false ;
}
}
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
for ( i = start ; i <= end ; i++ ) ...{
printf ( "%c", str[i] ) ;
}
printf ( " " ) ;
}
return 0 ;
}