NEUOJ第1155题 Mysterious Organization —— 顺便训练一下“正则表达式”
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
解题思路:纯粹的字符串匹配,没有任何难度。直接用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 1000010 int 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 1000010 int 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; }