NEUOJ第1155题 Mysterious Organization —— 顺便训练一下“正
NEUOJ第1155题,Mysterious Organization(题目链接)。
解题思路:纯粹的字符串匹配,没有任何难度。直接用C语言的strstr或者C++的STL中的string::find就能搞定。
查看C语言strstr版源代码
#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( "%dn",count ); return EXIT_SUCCESS; }
查看C++语言string::find版源代码
#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( "%dn",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,0 ); while ( cin >> URL ) { if ( regexec( &preg,URL.c_str(),0) == 0 ) count ++; } regfree( &preg ); cout << count << endl; return EXIT_SUCCESS; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |