以I和O作为出栈和入栈操作的缩写,若栈的终态和始态都为空,判断一个序列是否合法
#include <stdio.h>
#include <stdlib.h>
int judge();
int main(int argc, char** argv){
char a[8];
int i=0;
for(i=0;i<8;i++)
{
scanf("%c",&a[i]);
}
judge(a);
return 0;
}
int judge(char a[])
{
int i=0;
int j=0;
int k=0;
while(a[i]!=’\0’){
switch(a[i]){
case ‘I’: j++;
break;
case ‘O’:k++;
if(k>j)
{
printf(“非法序列\n”);
exit(0);
}break;
}
i++;
}
if(j!=k)
{
printf(“非法序列\n”);
return 1;
}
else
{
printf(“合法序列\n”);
return 0;
}
}