【一隻蒟蒻的刷題歷程】 【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;
}

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