一题DP题
输入一串由'(',')','[',']'组成的字符串,对于[ ],( ),[ ( ) ], ( [ ] ),都是合法的匹配。而对于( ],[ )等则是不合法的匹配,你的任务是找出括号集中最长合法匹配
我们先处理只有'('和')'的情况,遇到一个')',如果前面是一个'(',那么长度是2的"()"肯定是可以的了,但是如果'('前面已经有匹配了的,如前面如果是一个已经匹配了的"()",那么我们需要把它也加进去.
如上,我们再把'['和']'加进来

/**//*
0.13s
DP
*/
#include "stdio.h"
#include <string.h>

char str[100002] ;
int DP[100002] ;
bool mark[100002] ;

int main ( void )

...{
int i, j ;

while ( 1 == scanf ( "%s", str ) ) ...{
int start = 0, end = -1, len = -1 ;
DP[0] = -1 ; mark[0] = false ;
int pos = strlen(str) ;

for ( i = 1 ; i < pos ; i++ ) ...{

if ( str[i] == '(' || str[i] == '[' ) ...{
mark[i] = false ;
//DP[i] = -1 ;
}

else if ( str[i] == ')' ) ...{

if ( str[i-1] == '(' ) ...{

if ( i-2 >= 0 && mark[i-2] ) ...{
mark[i] = true ;
DP[i] = DP[i-2] ;
}

else ...{
mark[i] = true ;
DP[i] = i-1 ;
}

if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}

else if ( mark[i-1] && DP[i-1] != 0 ) ...{

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 ;

if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}
else mark[i] = false ;
}
else mark[i] = false ;
}

else if ( str[i] == ']' ) ...{

if ( str[i-1] == '[' ) ...{

if ( i-2 >= 0 && mark[i-2] ) ...{
mark[i] = true ;
DP[i] = DP[i-2] ;
}

else ...{
mark[i] = true ;
DP[i] = i-1 ;
}

if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}

else if ( mark[i-1] && DP[i-1] != 0 ) ...{

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 ;

if ( len < i-DP[i] ) ...{
len = i - DP[i] ;
start = DP[i] ;
end = i ;
}
}
else mark[i] = false ;
}
else mark[i] = false ;
}
}

for ( i = start ; i <= end ; i++ ) ...{
printf ( "%c", str[i] ) ;
}
printf ( " " ) ;
}
return 0 ;
}