做解析器做得多的我,一上来就觉得要写解析器,麻烦,于是想偷懒用java的正则表达式类Pattern直接进行判断,结果wa了,原因是这题要求的正则表达式只是真正正则表达式的一个子集。比如|12是合法正则表达式,但是此题中属于不合法。还是把代码贴上吧:

import java.util.Scanner;
import java.util.regex.Pattern;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
boolean res;
while(cin.hasNextLine()) {
String line = cin.nextLine();
res = true;
try {
Pattern.compile(line);
}catch(Exception e) {
res = false;
}
System.out.printf("%s\n", res ? "yes" : "no");
}
}
}

然后就乖乖用c++写递归下降了,写着写着发现挺简单,就一个函数自递归就好了,so easy~~

/*
* Author : ben
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <deque>
#include <list>
#include <functional>
#include <numeric>
#include <cctype>
using namespace std;
int get_str(char *str) {
char c;
while ((c = getchar()) <= ' ') {
if(c == EOF) {
return -;
}
}
int I = ;
while (c > ' ') {
str[I++] = c; c = getchar();
}
str[I] = ;
return I;
}
int len, c;
char str[]; void regex() {
if (c >= len || (str[c] != '' && str[c] != '' && str[c] != '(')) {
throw ;
}
if (str[c] == '(') {
c++;
do {
regex();
} while (str[c] != ')');
c++;
} else {
while (str[c] == '' || str[c] == '') {
c++;
}
}
while (str[c] == '*') {
c++;
}
if (str[c] == '|') {
c++;
regex();
}
} int main() {
while ((len = get_str(str)) > ) {
c = ;
try {
while (c < len) {
regex();
}
} catch(int) {
printf("no\n");
continue;
}
printf("yes\n");
}
return ;
}

bjfu1284 判别正则表达式的更多相关文章

  1. linux 正则表达式与文件格式化处理

    此文涉及命令:grep.sed.awk.printf.diff.cmp.patch. 概念: 什么是正则表示法 简单的说,正则表示法就是处理字符串的方法,他是以行为单位来进行字符串的处理行为, 正则表 ...

  2. 鸟哥的linux私房菜——第12章 正则表达式与文件格式化处理

    12.1什么是正则表达式 正则表达式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊符号的辅助,可以让用户轻易达到查找.删除.替换某特定字符串的处理程序. vi.gr ...

  3. bash shell学习-正则表达式基础 (笔记)

    A gentleman is open-minded and optimistic; a small person is narrow-minded and pessimistic. "君子 ...

  4. sed正则表达式

    sed的正则匹配如何实现非贪婪? sed的正则用的是BREs/EREs,不支持非贪婪模式.当然有一些方法可以实现非贪婪,比如: $ echo abcOabcdOabc | sed 's/.*O//' ...

  5. Linux正则表达式grep与egrep

    grep -io "http:/=[A-Z0-9]\{16\}" ./wsxf.txt >wsxf_urls.txt Linux正则表达式grep与egrep 正则表达式:它 ...

  6. Linux通配符与基础正则表达式、扩展正则表达式

    在Linux命令行操作或者SHELL编程中总是容易混淆一些特殊字符的使用,比如元字符‘*’号,作为通配符匹配文件名时表示0个到无穷多个任意字符.而作为正则表达式匹配字符串时,表示重复0个到无穷多个的前 ...

  7. 正则表达式校验15/18位生份证-JAVA版

    public static boolean isIDNumber(String iDNumber) { if (iDNumber == null || "".equals(iDNu ...

  8. C#中正则表达式编程(未完,待补充)

    对于只存储一个匹配,可用Match类: 一般模式: Regex reg = new Regex(string pattern); string str = "###############& ...

  9. Linux学习-延伸正则表达式

    grep 默认仅支持基础正则表达式,如果要使用延伸型正则 表达式,你可以使用 grep -E , 不过更建议直接使用 egrep !直接区分指令比较好记忆!其 实 egrep 与 grep -E 是类 ...

随机推荐

  1. BFS 模板

    转自:欣哥 下面是bfs一般的形式,谈不上模板但一般都这么来做有点乱有什么多交流 bfs一般用于求最短时间 #include<stdio.h>#include<queue>us ...

  2. NAMESPACE_ERR: An attempt is made to create or change an object in a way which is incorrect with regard to namespaces.

    解决办法: http://stackoverflow.com/questions/4037125/namespace-err-an-attempt-is-made-to-create-or-chang ...

  3. 利用Nginx搭建http和rtmp协议的流媒体服务器

    http://www.linuxidc.com/Linux/2013-02/79118.htm

  4. Linux系统与性能监控

    原文地址:http://kerrigan.sinaapp.com/post-7.html Linux System and Performance Monitoring http://www.hous ...

  5. IDEA快速光标跳转

    Ace Jump是一种从emacs上借鉴过来的快速光标跳转方式,操作方式是:你用某个快捷键进入Ace Jump模式后,再按任一个键,当前屏幕中所有该字符都被打上一个字母标记,你只要按这个字母,光标就会 ...

  6. 一个不错的log4j.properties例子

    # Set root logger level to WARN and append to stdout #在开发环境下日志级别要设置成DEBUG,生产环境设置成info或error log4j.ro ...

  7. 89. Gray Code

    题目: The gray code is a binary numeral system where two successive values differ in only one bit. Giv ...

  8. 基于Jws的WebService项目

    基于Jws的WebService项目   1.服务器端建立 1.1.创建接口 [java] view plaincopy @WebService  public interface IWebServi ...

  9. 虚函数(virtual)为啥不能是static

    静态成员函数,可以不通过对象来调用,即没有隐藏的this指针. virtual函数一定要通过对象来调用,即有隐藏的this指针. static成员没有this指针是关键!static function ...

  10. Objective-c CoreData

    #import "AppDelegate.h" #import "Person.h" @implementation AppDelegate @synthesi ...