NEUOJ第1155题 Mysterious Organization —— 顺便训练一下“正则表达式”
来源:互联网 发布:美橙域名证书下载 编辑:程序博客网 时间:2024/05/17 08:34
NEUOJ第1155题,Mysterious Organization(题目链接)。
Mysterious Organization
Description
GFW had intercepted billions of illegal links successfully. It has much more effect. Today, GFW intercepted a message of a mysterious organization. This mysterious organization package the message in legitimate URL.
To find the Black Hand behind the scenes, GFW didn’t shield the IP. Instead, it chooses to tap the massage. Because the messages are packaged in URL, and encrypted by the way we don’t know, GFW chooses to monitor all the URL passed, to get enough information to find the Black Hand behind the scenes.
This kind of URL is easy to find. This kind of URL including the word’ manure’. Your task is to find how many URL is this kind.
Considering traveling in it, you are free to any cell containing a composite number or 1, but traveling to any cell containing a prime number is disallowed. You can travel up, down, left or right, but not diagonally. Write a program to find the length of the shortest path between pairs of nonprime numbers, or report it's impossible.
Input
Input has only one case, it has multiple lines. Each line has a string standing for the URL passing GFW. The length of the string is less than 256 without blank in it. There won’t be more than 256 lines in the cases.
Output
Output one line standing for the number of URL GFW needs.
Sample Input
https://61.135.169.105//manurer//whoistheboss
http://61.135.169.105//manur//whoistheboss
http://61.135.169.105//mare//whoistheboss
https://61.135.169.105//manure//Iamtheboss
Sample Output
2
Source
2012黑龙江省赛
解题思路:纯粹的字符串匹配,没有任何难度。直接用C语言的strstr或者C++的STL中的string::find就能搞定。
#include <stdio.h>#include <stdlib.h>#include <string.h>int main (void){ int count = 0; char URL[1000]; while ( gets(URL) != NULL ) { if (strstr(URL, "manure" ) ) count ++; } printf( "%d\n", count ); return EXIT_SUCCESS;}
#include <iostream>#include <cstdlib>#include <string>using namespace std;int main (void){ int count = 0; string URL; while ( cin >> URL ) { if ( URL.find( "manure" ) != string::npos ) count ++; } cout << count << endl; return EXIT_SUCCESS;}
当然,如果只因为这个发表解题报告,没有任何意义。我发表这篇解题报告的目的是训练一下“正则表达式”的使用。
Java语言源代码如下:
import java.io.*;import java.util.*;import java.util.regex.Matcher;import java.util.regex.Pattern;public class Main{ public static void main ( String args[] ) { String URL; int count = 0; Scanner cin = new Scanner(System.in); Pattern pattern = Pattern.compile(".*manure.*"); while ( cin.hasNext()) { URL = cin.nextLine(); Matcher matcher = pattern.matcher(URL); if ( matcher.matches()) count ++; } System.out.println( count ); }}
以上代码用的是Java正则表达式。
以下两个代码用的是GNU正则表达式,所以仅能用gcc/g++编译,用其它编译器会编译错误哦!
C语言源代码如下:
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <regex.h>#define MAX_LENGTH 1000010int main (void){ char URL[1000]; int count = 0; regex_t preg; regcomp( &preg, ".*manure.*", 0 ); while ( gets(URL) != NULL ) { if ( regexec( &preg, URL, 0, NULL, 0) == 0 ) count ++; } printf( "%d\n", count ); regfree( &preg ); return EXIT_SUCCESS;}
C++语言源代码如下:
#include <iostream>#include <cstdlib>#include <string>#include <regex.h>using namespace std;#define MAX_LENGTH 1000010int main (void){ int count = 0; regex_t preg; string URL; regcomp( &preg, ".*manure.*", 0 ); while ( cin >> URL ) { if ( regexec( &preg, URL.c_str(), 0, NULL, 0) == 0 ) count ++; } regfree( &preg ); cout << count << endl; return EXIT_SUCCESS;}
- NEUOJ第1155题 Mysterious Organization —— 顺便训练一下“正则表达式”
- Mysterious Organization
- Mysterious Organization
- 黑龙江省第七届大学生程序设计竞赛-Mysterious Organization
- 犀牛 ——第10章 正则表达式的模式匹配 10.1 正则表达式的定义
- 《精通正则表达式》学习笔记——第1章:正则表达式入门
- JavaScript 正则表达式记录一下
- 8.13 爬虫训练-正则表达式
- 顺便说一下
- Java正则表达式的学习和笔记——黑马训练营
- 剑指offer | 训练题51:正则表达式匹配
- 正则表达式第一篇
- 正则表达式第一天
- 正则表达式—-转义
- 正则表达式—叠词
- Javascript—正则表达式
- Javascript—正则表达式
- 正则表达式—Email
- 大神与三位小伙伴问题解法
- 杭电OJ第4245题 A Famous Music Composer
- 学习4-Cocos2D-X UI系统
- 杭电OJ第4256题 The Famous Clock
- 杭电OJ第4255题 A Famous Grid
- NEUOJ第1155题 Mysterious Organization —— 顺便训练一下“正则表达式”
- 吉林大学OJ第2775题 Problem F: Shadows
- 杭电OJ第4018题 Parsing URL
- 杭电OJ第4011题 Working in Beijing
- 杭电OJ第4015题 Mario and Mushrooms
- ACM-ICPC Live Archive 第4889题 Post Office
- BNU OJ 第26303 题 Touchscreen Keyboard
- C++ 11 STL 线程库实现的线程同步与互斥
- C++11标准 STL正则表达式 验证电子邮件地址