• 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;
    } 
    

      

  • 相关阅读:
    flex 布局
    5个有用的 CSS 布局生成器
    js 函数
    js 类定义的方法(最终)
    js && ||
    css position 盒子模型
    eldatepicker选择时间,限定选择的时间段
    Java基础学习总结——Java对象的序列化和反序列化
    pytorch自定义算子
    网站上视频下载后保存为MP4格式
  • 原文地址:https://www.cnblogs.com/grglym/p/7867283.html
Copyright © 2020-2023  润新知