zoukankan      html  css  js  c++  java
  • 1071. Speech Patterns (25)

    People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.

    Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?

    Input Specification:

    Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return ' '. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

    Output Specification:

    For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

    Note that words are case insensitive.

    Sample Input:

    Can1: "Can a can can a can?  It can!"
    

    Sample Output:

    can 5

    解题思路:主要是map的用法。

    #include<iostream>
    #include<vector>
    #include<map>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    map<string,int>mp;
    vector<char>v;
    bool legal(int i){
    	char c = v[i];
    	if(c>='A'&&c<='Z'){
    		v[i]=c-'A'+'a';
    		return true;
    	}else if(c>='a'&&c<='z'){
    		return true;
    	}else if(c>='0'&&c<='9'){
    		return true;
    	}else{
    		return false;
    	}
    }
    int main(){
    	char ch;
    	while(scanf("%c",&ch)){
    		v.push_back(ch);
    		if(ch=='
    '){
    			break;
    		}
    	}
    	int len=v.size();
    	int tmp=0;
    	int s=0,d=0;
    	int max=-1;
    	vector<char>::iterator it = v.begin();
    	for(int i=0;i<len;i++){
    		if(legal(i)){
    			d++;
    			continue;
    		}
    		if(s<d){
    			string ss(it+s,it+d);
    			++mp[ss];
    			if(mp[ss]>max){
    				max=mp[ss];
    			}
    			d++;
    			s=d;
    			continue;
    		}
    		s = d = i+1;
    	}
    	map<string,int>::iterator ite = mp.begin();
    	for(;ite != mp.end();ite++){
    		if(ite->second == max){
    			//printf("%s %d
    ",ite->first,ite->second);
    			cout<<ite->first<<' '<<ite->second<<endl;
    			break;
    		}
    	}
    	return 0;
    } 
    

      

  • 相关阅读:
    安装wampserver2时出现的问题
    微信相关信息
    YII CDbCriteria总结
    discuz@功能的代码
    音乐搜索并生成播放功能
    php生成json和js解析json
    Discuz!提取文章标签
    ⑦ vue项目结构study
    ⑤ elementui 使用字符填充table空白表格项
    ④ keep-alive缓存组件,操作之后需要重新获取数据--activated
  • 原文地址:https://www.cnblogs.com/grglym/p/7867283.html
Copyright © 2011-2022 走看看