【一只蒟蒻的刷题历程】 【PAT】 A1071 语音模式

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 \n. 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


题目大意:

人们经常会偏爱同一单词的同义词。例如,有些人可能更喜欢“警察”,而另一些人可能更喜欢“警察”。分析这样的模式可以帮助缩小说话者的身份,这在验证例如在线头像后面是否仍然是同一个人时非常有用。

现在给定一段从某人讲话中提取的文字,您能找到该人最常用的词吗?

输入规格:

每个输入文件包含一个测试用例。对于每种情况,一行文字的长度均不超过1048576个字符,并以回车符\ n终止。输入内容至少包含一个字母数字字符,即[0-9 A-Z a-z]中的一个字符。

输出规格:

对于每个测试用例,请在一行中打印输入文本中最常出现的单词,后跟一个空格和该单词在输入中出现的次数。如果此类单词不止一个,请按字典顺序打印最小的单词。该单词应全部小写。此处,“单词”定义为由非字母数字字符或行首/结尾分隔的字母数字字符的连续序列。

请注意,单词不区分大小写。

样本输入:

Can1: “Can a can can a can? It can!”

样本输出

can 5


思路:

首先,把无效字符都给变成字符串,那么空格前面(前面无空格)就是一个单词,如果该空格前面有空格,就不能构成一个单词,记录下次数即可。(大致思路,详情见代码)

代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;  
bool pan(char c)  //判断是否属于有效的字符
{
	if((c>='0' && c<='9') || (c>='a' && c<='z') || (c>='A' && c<='Z'))
	 return 1;
	else return 0;
}
int main() 
{
  map<string,int> m; //记录出现字符串的次数 
  vector<string> v;  //记录出现的字符串 
  string s,temp;   //s输入语句 , temp记录哪几个字符构成一个单词
  getline(cin,s);
  
  int flag1=0; //最后一个测试点,判断是否有空格(可能只说了一个词)
  for(int i=0;i<s.size();i++)
  {
  	 if(s[i]==' ') flag1=1; //有空格
  	 if(!pan(s[i]))   //把无效字符都变成空格
	    s[i] = ' ';
  }
  if(flag1==0) //无空格,只说了一个词,直接输出
  {
  	cout<<s<<" "<<1;
  	return 0;
  }
  int flag=0; 
  //记录该字符前一个字符是否为空格,可能有连续多个空格
  //如果空格前面还是空格 那么前面就不是一个单词,跳过即可
  for(int i=0;i<s.size();i++)
  {
  	 if(s[i]!=' ')  //不为空格
	 {
	 	flag=0;
	 	temp += tolower(s[i]);  //加入当前单词
	 }
  	 else  //为空格
	 {
	 	if(flag==1) continue;
	 	 //空格前一个字符是空格,前面不构成单词 跳过
	 	else
	 	{
	 		flag=1; //标记为空格
	 		m[temp]++;  //记录该单词出现的次数
  	    	v.push_back(temp); //记录出现的单词
  	 	    temp.clear(); //清空当前单词的字符串
		}
	 } 
  }
  
  string ans;  //出现最多的单词
  int cnt=-1;  //出现最多单词的次数
  for(int i=0;i<v.size();i++) //出现过的单词遍历一遍
  {
  	 if(m[v[i]] > cnt)  //次数比cnt多
  	 {
  	 	ans = v[i];  //更新单词
  	 	cnt = m[v[i]]; //更新次数
	 }
  }
  cout<<ans<<" "<<cnt; //输出
  return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章