【1071】Speech Patterns (25 分)

简介: 【1071】Speech Patterns (25 分)【1071】Speech Patterns (25 分)
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string>
#include<algorithm>  
#include<map>
#include<vector>
#include<queue> 
using namespace std;  
//题目是统计出现最多的单词,key:分割给定字符串后map计数
bool check(char c){ //检查字符c是否为[0,9] [A,Z] [a,z]
  if(c>='0'&&c<='9') return true;
  if(c>='a'&&c<='z') return true;
  if(c>='A'&&c<='Z') return true;
  return false;
}
int main(){   
   map<string, int> count;  //count计数字符串出现的次数
   string str;
   getline(cin,str);  //读入整行字符串
   int i=0;  //定义下标
  while(i<str.length()){  //在字符串范围内
    string word;  //单词
    //下面这个while分割出一个个单词并map
    while(i<str.length()  &&  check(str[i])==true ){//如果是单词的话 ,注意第一个条件
      if(str[i] >= 'A' && str[i] <='Z'){ 
        str[i]+=32;   //将大写字母转换为小写字母
      }
      word+=str[i] ;//单词末尾添加该字符,之前没写过这种。。
      i++; //下标后移1位
    }
      if(word!=""){  //单词非空,令次数加1
        if(count.find(word) == count.end() )  count[word]=1; //单词不存在,count赋值为1
        else count[word]++;
      }
      while(i<str.length()    &&    check(str[i])==false) { //注意加第一个条件,否则会爆内存
        i++;   //跳过非单词字符
      }
  } //大外层while结束
  string ans;  //存放出现次数最多的单词
  int MAX=0;  //出现最多的单词的次数
  for(map<string,int>::iterator it=count.begin()  ; it!=count.end() ; it++){
    if(it->second > MAX){  //寻找出现次数最多的单词
      MAX=it->second;
      ans=it->first;
    }
  }
  cout<<ans<<" "<<MAX;
  system("pause");
    return 0;   
}
相关文章
|
4月前
|
机器学习/深度学习 自然语言处理 算法
【论文精读】ACL 2022:Graph Pre-training for AMR Parsing and Generation
【论文精读】ACL 2022:Graph Pre-training for AMR Parsing and Generation
|
6天前
|
机器学习/深度学习 自然语言处理 算法
词性标注(Part-of-Speech Tagging)
词性标注(Part-of-Speech Tagging)
|
机器学习/深度学习 自然语言处理 算法
ACL 2022:Graph Pre-training for AMR Parsing and Generation
抽象语义表示(AMR)以图形结构突出文本的核心语义信息。最近,预训练语言模型(PLM)分别具有AMR解析和AMR到文本生成的高级任务。
138 0
|
机器学习/深度学习 人工智能 自然语言处理
One SPRING to Rule Them Both Symmetric AMR Semantic Parsing and Generation without Complex Pipeline
在文本到AMR解析中,当前最先进的语义解析器集成了几个不同模块或组件的繁琐管道,并利用图重新分类,即在训练集的基础上开发的一组特定内容的启发式方法。
116 0
|
C++
【PAT甲级 - C++题解】1071 Speech Patterns
【PAT甲级 - C++题解】1071 Speech Patterns
59 0
|
C语言 C++
PAT (Basic Level) Practice (中文)1099 性感素数(20分)
“性感素数”是指形如 (p, p+6) 这样的一对素数。之所以叫这个名字,是因为拉丁语管“六”叫“sex”(即英语的“性感”)。(原文摘自 http://mathworld.wolfram.com/SexyPrimes.html) 现给定一个整数,请你判断其是否为一个性感素数。
125 0
|
算法 Linux 测试技术
【论文阅读】(2014)Combinatorial Benders’ Cuts for the Strip Packing Problem
【论文阅读】(2014)Combinatorial Benders’ Cuts for the Strip Packing Problem
217 0
【论文阅读】(2014)Combinatorial Benders’ Cuts for the Strip Packing Problem
|
机器学习/深度学习 自然语言处理 算法
Re4:读论文 CGSum: Enhancing Scientific Papers Summarization with Citation Graph
Re4:读论文 CGSum: Enhancing Scientific Papers Summarization with Citation Graph
Re4:读论文 CGSum: Enhancing Scientific Papers Summarization with Citation Graph
|
测试技术
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
99 0
PAT (Basic Level) Practice (中文) B1011 A+B 和 C (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
PAT (Basic Level) Practice (中文) 1016 部分A+B (15 分)
79 0