題目的鏈接在這 http://acm.nyist.net/JudgeOnline/problem.php?pid=2
題目如下:
描述:
現在,有一行括號序列,請你檢查這行括號是否配對。
輸入:
第一行輸入一個數N(0<N<=100),表示有N組測試數據。後面的N行輸入多組輸入數據,每組輸入數據都是一
個字符串S(S的長度小於10000,且S不是空串),測試數據組數少於5組。數據保證S中只含有"[","]","(",")"四
種字符。
輸出:
每組輸入數據的輸出占一行,如果該字符串中所含的括號是配對的,則輸出Yes,如果不配對則輸出No。
樣例如下:
3
[(])
(])
([[]()])
No
No
Yes
我做的代碼如下:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
int n;
char str[10000];
void deal( char str[]);
scanf("%d",&n);
while(n--)
{
scanf("%s",str);
deal( str);
}
}
void deal( char str[])
{
int len = strlen( str);
int i,j,n,flag;
if( len%2 != 0 || str[0] == ')' || str[0] == ']' )
{
printf("No\n");
}
else
{
for( i = 0; i < len; i++)
{
if( str[i] == ')' || str[i] == ']')
{
for( j = i-1; j > -1; j--)
{
if( str[j] == '0' )
continue;
if( (int)(str[i] - str[j]) != 1 && (int)(str[i] - str[j]) != 2)
{
printf("No\n");
return;
}
else
{
str[i] = str[j] = '0';
break;
}
}
}
}
if( str[ len-1] == '0' )
printf("Yes\n");
}
}