PAT:1071. Speech Patterns (25) AC

#include<ctype.h>
#include<iostream>
#include<map>
#include<string>
using namespace std;

bool check(char a)          //判断是否为有效字符
{
  if(isdigit(a) || isalpha(a))
    return true;
  return false;
}
int main()
{
  map<string,int> count;    //单词与次数的映射
  string str;
  getline(cin,str);      //【skill】输入一行,不能用gets
  int i=0;
  while(i<str.length())    //【caution】这里不是size
  {
    while(i<str.length() && check(str[i])==false)    //忽略非有效字符
      ++i;
    string word;
    while(i<str.length() && check(str[i])==true)    //抠出单词
    {
      if(str[i]>=‘A‘ && str[i]<=‘Z‘)
        str[i]=str[i]-‘A‘+‘a‘;            //【caution】统计单词,大写要换成小写
      word+=str[i++];
    }
    if(count.find(word)!=count.end())          //统计词频
      ++count[word];
    else
      count[word]=1;
  }
  string ans;
  int MAXtimes=-1;
  for(map<string,int>::iterator it=count.begin() ; it!=count.end() ; ++it)
  {
    if(it->second>MAXtimes)
    {
      MAXtimes=it->second;
      ans=it->first;
    }
  }
  cout<<ans<<" "<<MAXtimes<<endl;
  return 0;
}
时间: 2024-08-07 00:33:34

PAT:1071. Speech Patterns (25) AC的相关文章

PAT 1071. Speech Patterns (25)

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 ident

PAT (Advanced Level) 1071. Speech Patterns (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

1071. Speech Patterns (25)

map的使用 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming 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

PAT:1059. Prime Factors (25) AC

#include<stdio.h> #include<math.h> #include<string.h> #include<algorithm> using namespace std; const int MAX=100010; //int型素数一定在这个范围内 int PrimeArr[MAX]; //素数表 int cnt=0; //素数表中素数个数 int x,x2; //输入的数字,x2是x的开平方数,x的因子只有从2到根号x+1 struct

PAT:1020. Tree Traversals (25) AC

#include<stdio.h> #include<stdlib.h> #include<queue> using namespace std; int POST[32]; //存放后序遍历 int IN[32]; //存放中序遍历 int n; //节点数 struct node { int data; node* l; node* r; }; node* creat(int postL,int postR,int inL,int inR) { if(postL&g

PAT:1063. Set Similarity (25) AC

#include<stdio.h> #include<set> using namespace std; set<int> str[51]; //最大51个集合 void compare(int s1,int s2) { int different=str[s1].size(),same=0; //初始化并集元素为s1元素个数,交集元素0个 for(set<int>::iterator it=str[s2].begin() ; it!=str[s2].end

PAT:1028. List Sorting (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Student { char ID[10]; char name[10]; int gread; }STU[100010]; bool cmp1(Student a,Student b) { return strcmp(a.ID,b.ID)<0; } bool cmp2(Student a,Stu

PAT:1015. 德才论 (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; struct Student { char mID[10]; int de,cai,sum; int tag; //标明是第几类:1德才都们组,2德胜才,3 }STU[100010]; bool cmp(Student a,Student b) //[warning]不能写STU { if(a.tag!=b.tag)

PAT:1083. List Grades (25) AC

#include<stdio.h> #include<algorithm> using namespace std; struct Student { char name[15]; char ID[15]; int gread; }STU[66666]; bool cmp(Student a,Student b) { return a.gread>b.gread; } int main() { int n; scanf("%d",&n); for(