codeup 6168 Speech Patterns

codeup 6168 Speech Patterns

题目描述
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?
输入
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 ‘\n’. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].
输出
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.
样例输入输出

Can1: "Can a can can a can?  It can!"  // can 5
yxy  YXY 123 123   // 123 2

思路
找出一行中出现频率最高的单词。用while 查找 除了 空格和非字母数字字符外,第一个字符位置,记为单词的起始(left),再用while 找到该单词的边界,即第一个不是字母数字的字符,记为单词的结束(right),这样就读取到了一个单词。

tempstr=inputstr.substr(left,right-left);

将其变小写后查找map中是否有该单词:

	it=ans.find(tempstr);

若该单词已存在,即it!=ans.end(),那么该单词对应的次数value加1,ans[tempstr]++;
若该单词不存在,即it==ans.end(),放入map,并置value=1,

由于未掌握有关map的排序,要设置tempstr,保留当前出现次数最多的单词,在每次对vaule更新时,都要与ans[tempstr].value 比较,若大于,则根线tempstr 的值。
AC代码

#include<cstdio>
#include<iostream>
#include<map>
#include<string>
#include<algorithm>
using namespace std;

pair<string,int> tempmap=make_pair(" ",0);   //保存当前出现次数最多的单词 
 
void cmp(string a,int b)  //比较出现次数大小 
{
	if(tempmap.second< b || (tempmap.second== b && tempmap.first > a)  )  //若次数大于当前最大次数 ,更新 
	{                                                                  //若相等,则取字典序小的 
		tempmap.second=b ;
		tempmap.first =a;
	}
}

string toLowerCase(string s)     //返回小写单词 
{
	for(int i=0;i<s.length();i++)
	{
		if(s[i]<'a' && s[i]>'9' )s[i]=s[i]-'A'+'a';
	}
	return s;
}

bool notlegal(char c)  //判断是否是非单词数字字符 
{
	if( (c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='z')) return false;
	else return true;
}

int main()
{
	map<string,int> ans;
	map<string,int>::iterator it;
	string inputstr;
	getline(cin,inputstr);
	int len=inputstr.length();

	int left=0,right=-1;
	string tempstr; 
	int i=0;
	while(i<len)
	{
		while( (inputstr[i]==' ' || notlegal(inputstr[i]) ) && i<len) i++;  //跳过空格和数字字母字符 
		
		left=i; //单词起始 
		
		while(inputstr[i]!= ' ' && i<len  && !notlegal(inputstr[i])) i++; //找到单词的结束 
		
		right=i; //单词的结束位置 
		tempstr=inputstr.substr(left,right-left); //获取一个单词 
		
		tempstr=toLowerCase(tempstr);  //变小写 

 
		it=ans.find(tempstr); //该单词是否已出现过 
		if(it!=ans.end())    //出现过 
		{
			ans[tempstr]++;
			cmp(tempstr,ans[tempstr]); //是否是当前出现次数最多的 
			
		}
		else  //未出现过 
		{
			 ans[tempstr]=1;
			 cmp(tempstr,ans[tempstr]); //是否是当前出现次数最多的 
		}
	}
	
	cout<<tempmap.first<<" "<<tempmap.second<<endl; //输出当前出现次数最多的 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值