zoukankan      html  css  js  c++  java
  • [洛谷] P1308 统计单词数

    题目描述

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

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

    中的某一独立单词在不区分大小写的情况下完全相同(参见样例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题

    基础的stl map容器操作

    创建两个容器

    一个记录是否出现与第一次出现位置

    另一个记录次数

    #include <iostream>
    #include <map>
    #include <cstdio>
    using namespace std;
    int main()
    {
    	map <string,int> words;
    	map <string,int> times;
    	int flag = 0;
    	string text,mode,tmp = "";
    	cin>>mode;
    	getchar();
    	getline(cin,text);
    	
    	for(int i=0;i<mode.length();i++)
    		if(mode[i] >= 'A' && mode[i] <= 'Z')
    			mode[i] = mode[i]+32;
    			
    	for(int i=0;i<text.length();i++)
    		if(text[i] >= 'A' && text[i] <= 'Z')  //统一小写化
    			text[i] = text[i] + 32;
    
    	//cout<<mode<<endl<<text<<endl;
    	for(int i = 0;i <= text.length();i ++)
    	{
    		if(text[i] != ' '&& text[i] != '')
    		{
    			tmp += text[i];
    		}
    		else if(text[i] == ' ' || text[i] == '')
    		{
    			//cout<<tmp<<flag<<endl;
    			if(! words.count(tmp))
    			{
    				words[tmp] = flag;
    				times[tmp] ++;
    				tmp = "";
    				flag = i + 1;
    			}
    			else
    			{
    				times[tmp] ++;
    				tmp = "";
    				flag = i + 1;
    			}
    		}
    	}
    	if(! words.count(mode))
    		cout<<"-1"<<endl;
    	else 
    	{
    		cout<<times[mode]<<' ';
    		cout<<words[mode]<<endl;
    	}
    		
    	return 0;
    }
  • 相关阅读:
    sizeof和strlen与带汉字字符的
    PS常用工具
    色彩原理和图层混合模式
    文字工具和栅格化
    CreateWaitableTimer和SetWaitableTimer函数(定时器)
    FMod终结篇
    理财达人五步走
    C++中的Union
    关于对于VCMFCATL的评论问题
    FMOD 快速上手
  • 原文地址:https://www.cnblogs.com/zeolim/p/12270749.html
Copyright © 2011-2022 走看看