洛谷P1308 统计单词数
2017-07-20 00:35
399 查看
题目描述
一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章
中的某一独立单词在不区分大小写的情况下完全相同(参见样例1 ),如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例2 )。
输入输出格式
输入格式:输入文件名为stat.in ,2 行。
第1 行为一个字符串,其中只含字母,表示给定单词;
第2 行为一个字符串,其中只可能包含字母和空格,表示给定的文章。
输出格式:
输出文件名为stat.out 。
只有一行,如果在文章中找到给定单词则输出两个整数,两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从 0 开始);如果单词在文章中没有出现,则直接输出一个整数-1。
输入输出样例
输入样例#1:To to be or not to be is a question
输出样例#1:
2 0
输入样例#2:
to Did the Ottoman Empire lose its power at that time
输出样例#2:
-1
说明
数据范围1≤ 单词长度≤10。
1≤ 文章长度≤1,000,000。
noip2011普及组第2题
WriteUp:
BF基础算法:
70分参考代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> using namespace std; char str[1000005] = {'\0'}; char temp_str[1000005] = {'\0'}; int first_pos = 0; int BF_search(const char str[] , const char pat[]){ int count = 0; int flag = 0; int i=0,j=0; int str_len,pat_len; str_len = strlen(str); pat_len = strlen(pat); while (i<str_len && j<pat_len) { for (j=0;j<pat_len && i+j < str_len && str[i+j] == pat[j];j++); if (j == pat_len) { if (!flag) { first_pos = i; flag = 1; } count++; } i++; j=0; } if (!flag) { return -1; } return count; } int main(void) { int count; int i,j; char pat[15] = {'\0'}; char temp_pat[15] = {'\0'}; gets(temp_pat); gets(temp_str); strcat(str," "); strcat(pat," "); strcat(str,temp_str); strcat(pat,temp_pat); strcat(str," "); strcat(pat," "); for (i=0 ; i < strlen(str) ; i++) { str[i] = tolower(str[i]); } for (j=0 ; j < strlen(pat) ; j++) { pat[j] = tolower(pat[j]); } count = BF_search(str,pat); if (count == -1) { printf("-1"); }else{ printf("%d %d",count,first_pos); } return 0; }
相关文章推荐
- 【搜索】洛谷 P1308 统计单词数
- 洛谷 P1308 统计单词数【字符串+模拟】
- 【洛谷1026】【NOIP2001】统计单词个数
- 洛谷P1026 统计单词个数
- 【洛谷1026】【NOIP2001】统计单词个数
- 洛谷1026 统计单词个数
- P1308 统计单词的个数
- 洛谷1308 单词统计
- 洛谷OJ-P1308 统计单词数-字符串匹配-string
- 洛谷 P1026 统计单词个数
- P1308 统计单词数
- 洛谷 P1026 [NOIP2001 T3] 统计单词个数
- P1308 统计单词数
- 洛谷-统计单词数-简单字符串
- 洛谷1026 统计单词个数
- P1308 统计单词数(#1 - 5)
- 洛谷 P1026 统计单词个数
- 判断是否为素数和统计字符串中单词的个数
- 开发可统计单词个数的Android驱动程序(3)
- 启动Spark Shell,在Spark Shell中编写WordCount程序,在IDEA中编写WordCount的Maven程序,spark-submit使用spark的jar来做单词统计