bjfu1284 判别正则表达式
做解析器做得多的我,一上来就觉得要写解析器,麻烦,于是想偷懒用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 判别正则表达式的更多相关文章
- linux 正则表达式与文件格式化处理
此文涉及命令:grep.sed.awk.printf.diff.cmp.patch. 概念: 什么是正则表示法 简单的说,正则表示法就是处理字符串的方法,他是以行为单位来进行字符串的处理行为, 正则表 ...
- 鸟哥的linux私房菜——第12章 正则表达式与文件格式化处理
12.1什么是正则表达式 正则表达式就是处理字符串的方法,它是以行为单位来进行字符串的处理行为,正则表达式通过一些特殊符号的辅助,可以让用户轻易达到查找.删除.替换某特定字符串的处理程序. vi.gr ...
- bash shell学习-正则表达式基础 (笔记)
A gentleman is open-minded and optimistic; a small person is narrow-minded and pessimistic. "君子 ...
- sed正则表达式
sed的正则匹配如何实现非贪婪? sed的正则用的是BREs/EREs,不支持非贪婪模式.当然有一些方法可以实现非贪婪,比如: $ echo abcOabcdOabc | sed 's/.*O//' ...
- Linux正则表达式grep与egrep
grep -io "http:/=[A-Z0-9]\{16\}" ./wsxf.txt >wsxf_urls.txt Linux正则表达式grep与egrep 正则表达式:它 ...
- Linux通配符与基础正则表达式、扩展正则表达式
在Linux命令行操作或者SHELL编程中总是容易混淆一些特殊字符的使用,比如元字符‘*’号,作为通配符匹配文件名时表示0个到无穷多个任意字符.而作为正则表达式匹配字符串时,表示重复0个到无穷多个的前 ...
- 正则表达式校验15/18位生份证-JAVA版
public static boolean isIDNumber(String iDNumber) { if (iDNumber == null || "".equals(iDNu ...
- C#中正则表达式编程(未完,待补充)
对于只存储一个匹配,可用Match类: 一般模式: Regex reg = new Regex(string pattern); string str = "###############& ...
- Linux学习-延伸正则表达式
grep 默认仅支持基础正则表达式,如果要使用延伸型正则 表达式,你可以使用 grep -E , 不过更建议直接使用 egrep !直接区分指令比较好记忆!其 实 egrep 与 grep -E 是类 ...
随机推荐
- POJ3253Babelfish
http://poj.org/problem?id=3253 就是一个哈夫曼树,题目下边有提示,所以题意还是好理解的 #include<cstdio> #include<cstrin ...
- 使用JQuery双击修改Table中Td
<html> <head> <meta http-equiv="Content-Type" content="text/html;chars ...
- 在Android里完美实现基站和WIFI定位
来自:http://www.cnblogs.com/coffeegg/archive/2011/10/01/2197129.html 众所周知的,在OPhone和大部分国产的Android定制机里不支 ...
- JS模块化编程
AMD:异步模块定义,适合客户端环境,不会阻塞运行.客户端受网络影响比较大. CommonJs:适用于服务器端规范,可以同步加载,只受硬盘读写的影响.
- sql 随笔 2015-06-30
清除多余字符 --清除多余字符 --' --char(9) 水平制表符 --char(10)换行键 --char(13)回车键 REPLACE( REPLACE( REPLACE(REPLACE([P ...
- 我的第一个jquery插件:下拉多选框
<!DOCTYPE HTML> <html> <head> <title> New Document </title> <meta n ...
- Java的String、StringBuffer和StringBuilder的区别
1.String 2.Stringbuffer 3.StringBuilder 4.三者之间的区别 5.使用策略 1.String public final class String implemen ...
- bat拷贝文件
最近在部署服务器的时候,需要用到把一个站点文件拷贝到其他站点.一个一个手动copy太累人了,写了个简单的批处理文件,基本能达到目的,具体怎么做呢: 1.把需要拷贝到各个站点的文件,单独放到一个目录下. ...
- ios绘图时的坐标处理
在iOS中,进行绘图操作时,一般主要是在UIView:drawRect中调用 UIGraphicsBeginImageContextWithOptions等一系列函数,有时候直接画图就行,比如UIIm ...
- ZOJ 1610 Count the Colors (线段树 成段更新)
题目链接 题意:成段染色,初始为0,每次改变一个区间的颜色,求最后每种颜色分别有多少段.颜色按照从 小到大输出. 分析:改变了代码的风格,因为看了学长的博客.直接用数组,可以只是记录节点的编号,因为节 ...