题目:
Your task is to judge whether the input is a legal regular expression.
A regular expression is defined as follow:
1: 0 and 1 are both regular expressions.
2: If P and Q are regular expressions, PQ is a regular expression.
3: If P is a regular expression, (P) is a regular expression.
4: If P is a regular expression, P* is a regular expression.
5: If P and Q are regular expressions, P|Q is a regular expression.
Input
The input contains multiple testcases.
Each test case is a single line with a string not containing space.The length of the string is less than 100.
Output
For each testcase, print yes if the input is a regular expression, otherwise print no.
Sample Input
010101101* (11|0*)* )*111
Sample Output
yes yes no
解题报告:这道题目就是已知合法的正则表达式,然后又有* | ( )等符号,需要继续判断操作后的正则表达式是否还是合法的。因为( )这俩需要有匹配的现象,还有 | 能够覆盖掉 | 前边的数字,根据这些就可以写出代码了。
ac代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn =1e3+10;
char str[maxn];
int main()
{
while(scanf("%s",str)!=EOF)
{
int flag=0,intflag=0;//intflag是判断符号前边是否存在数字,主要是为()准备的
char c=str[0];
int i=0;
int cnt=0;
while(c!='\0'&&c!='\n')
{
if(c=='(')
{
cnt++;
intflag=0;
}
else if(c==')')
{
if(!intflag)
{
flag=1;
break;
}
cnt--;
}
if(cnt<0)
{
flag=1;
break;
}
if(c=='0'||c=='1')
{
intflag=1;
}
if(c=='*'&&!intflag)
{
flag=1;
break;
}
if(c=='|')
{
if(!intflag)
{
flag=1;
break;
}
intflag=0;
}
c=str[++i];
}
if(cnt!=0) flag=1;
if(flag)
printf("no\n");
else
printf("yes\n");
}
return 0;
}