SDUT 2134 資料結構實驗之棧與佇列四:括號匹配
阿新 • • 發佈:2019-02-11
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
const int MAXN=10000;
char s[MAXN];
char st[MAXN];
int main()
{
while(gets(s))
{
int top=0;
int i;
int l=strlen(s);
for(i=0;i<l;++i)
{
if(s[i]=='('||s[i]=='['||s[i]=='{')st[top++]=s[i];
else if(s[i]==')'||s[i]==']'||s[i]=='}')
{
if(s[i]==st[top-1]+1||s[i]==st[top-1]+2)
{
--top;
}
else
{
break;
}
}
}
if(i==l&&!top)puts("yes");
else puts("no");
}
return 0;
}
不明白這段程式碼為什麼提交會不正確-_-!
求大神指點~~~
#include<cstdio>
#include<cstring>
using namespace std;
char str[100],po[100];
int main()
{
while(scanf("%s",str)!=EOF)
{
int len=strlen(str);
int cnt=0;
int i;
for(i=0;i<len;i++)
{
if(str[i]=='('||str[i]=='['||str[i]=='{')
po[cnt++]=str[i];
else if(str[i]==')'||str[i]==']'||str[i]=='}')
{
if(str[i]==po[cnt-1]+1||str[i]==po[cnt-1]+2)
cnt--;
else
break;
}
}
if(i==len&&!cnt)
printf("yes\n");
else
printf("no\n");
}
return 0;
}