• P1308 统计单词数


    题目描述

    一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位置,有的还能统计出特定单词在文章中出现的次数。

    现在,请你编程实现这一功能,具体要求是:给定一个单词,请你输出它在给定的文章中出现的次数和第一次出现的位置。注意:匹配单词时,不区分大小写,但要求完全匹配,即给定单词必须与文章

    中的某一独立单词在不区分大小写的情况下完全相同(参见样例1 ),如果给定单词仅是文章中某一单词的一部分则不算匹配(参见样例2 )。

    输入输出格式

    输入格式:

    共22行。

    第11行为一个字符串,其中只含字母,表示给定单词;

    第22行为一个字符串,其中只可能包含字母和空格,表示给定的文章。

    输出格式:

    一行,如果在文章中找到给定单词则输出两个整数,两个整数之间用一个空格隔开,分别是单词在文章中出现的次数和第一次出现的位置(即在文章中第一次出现时,单词首字母在文章中的位置,位置从00 开始);如果单词在文章中没有出现,则直接输出一个整数-1−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:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    
    using namespace  std;
    
    string word;
    bool myfun(string str) {
    	return str == word ? true : false;
    }
    
    int main()
    {
    	string page;
    	vector<string> data;
    	getline(cin, word);
    	getline(cin, page);
    	// 全部转换小写 
    	transform(page.begin(), page.end(), page.begin(), ::tolower);
    	transform(word.begin(), word.end(), word.begin(), ::tolower);
    
    	string temp;
    	for (auto it = page.begin(); it != page.end(); it++) {	
    		if (*it == ' ') {
    			data.push_back(temp);
    			temp = "";
    		}else {
    			temp += *it;
    		}
    	}
    	if (!temp.empty()) data.push_back(temp);
    
    	int count = count_if(data.begin(), data.end(), ::myfun);
    	if (count == 0) {
    		cout << "-1" << endl;
    	}
    	else {
    		int pos = 0;
    		for (auto it = data.begin(); it != data.end(); it++) {
    			if (*it == word) {
    				pos  = pos + (it - data.begin());
    				break;
    			}else {
    				pos += it->length();
    			}
    		}
    		cout << count << " " << pos << endl;
    	}
    	return 0;
    }
    

    法2:

    #include <iostream>
    #include <string>
    using namespace  std;
    int main()
    {
    	string page;
    	string word;
    
    	getline(cin, word);
    	getline(cin, page);
    
    	// 全部转换小写 
    	transform(page.begin(), page.end(), page.begin(), ::tolower);
    	transform(word.begin(), word.end(), word.begin(), ::tolower);
    
    	string temp;
    	int pos = 0;
    	int count = 0;
    	for (auto it = page.begin(); it != page.end(); it++) {	
    		if (count == 0) pos++;
    		if (*it == ' ') {
    			if (temp == word)
    				count++;
    			temp = "";
    		}else {
    			temp += *it;
    		}
    	}
    	if (temp == word) {
    		if (count == 0) pos++;
    		count++;
    	}
    	if (count == 0) {
    		cout << "-1" << endl;
    	}
    	else {
    		pos -= word.length()+1;
    		cout << count << " " << pos << endl;
    	}
    	return 0;
    }
    
  • 相关阅读:
    KafkaOffsetMonitor
    锋利的KATANA
    用grunt搭建自动化的web前端开发环境
    网上书店订单功能的实现
    作用域和控制器
    使用CLK.AspNet.Identity提供以角色为基础的访问控制(RBAC)
    ABP日志管理
    .NET开源项目
    服务总线
    Message解析流程(转)
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12537728.html
Copyright © 2020-2023  润新知