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

      

  • 相关阅读:
    Java性能小技巧
    使用Gitolite搭建Gitserver
    refresh的停车场(栈和队列的STL)
    BZOJ 2005 NOI2010 能量採集 数论+容斥原理
    PHP第四课 了解经常使用的函数
    JavaScript实现对象数组按不同字段排序
    android之Context对各种服务的管理
    程序员应该阅读的非编程类书籍有哪些?
    是男人就下100层【第五层】——换肤版2048游戏
    是男人就下100层【第四层】——Crazy贪吃蛇(3)
  • 原文地址:https://www.cnblogs.com/grglym/p/7867283.html
Copyright © 2011-2022 走看看