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

      

  • 相关阅读:
    js原型对象与Java类的比较
    javascript特效--制作背景电子钟(整点时祝贺生日快乐)
    Java web MVC开发模式入门感悟
    重新审视面向对象编程
    10-排序4 统计工龄(20 分)
    基数排序
    表排序
    快速排序
    09-排序3 Insertion or Heap Sort(25 分)
    09-排序2 Insert or Merge(25 分)
  • 原文地址:https://www.cnblogs.com/grglym/p/7867283.html
Copyright © 2011-2022 走看看