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

      

  • 相关阅读:
    SecureCRT
    Jsoup 标签选择器 选择img标签中src的值
    使用Jsoup 爬取网易首页所有的图片
    java自定义类型 比较排序 Comparator接口
    eclipse下导入jdk源码
    java爬虫--使用正则表达式获取网页中的email
    Java正则表达式--Matcher.group函数的用法
    使用org.jsoup.Jsoup下载网络中的图片
    Tomcat中的Session小结
    关于JAVA中的static方法、并发问题以及JAVA运行时内存模型
  • 原文地址:https://www.cnblogs.com/grglym/p/7867283.html
Copyright © 2011-2022 走看看