当前位置: 首页 > ai >正文

HOJ.单词统计

题目

一段英语短文的内容记录于 lines 中,每行输入 lines[i] 仅包含 a-z , . , -,即英文小写字母,空格,逗号,句号和续行符。

请统计单词数量:

  • - 表示续行符(仅会出现在行尾,且不会出现在最后一行)
  • 对于含有续行符的行:将下一行的首个非空字符起的余下部分,拼接在当前行续行符左侧的字符后;
  • 对于不含续行符的行:与下一行是分隔的,相当于有一个单词分隔符。

单词:全部由字母组成

其它均为单词分隔符:包括一个或多个连续的空格、逗号、句号 。

输入

  • 1 <= lines.length <= 25
  • 1 <= lines[i].length <= 80

输出

一个整数,表示所统计的单词数量

算法标签: 模拟, 字符串操作

思路

整体算法分为三步

  1. 将带有续行符的单词进行合并
  2. 将字符串中的其他分隔符替换为空格, 并将字符串数组合并为一个单词字符串
  3. 将处理完的字符串放入 i s t r i n g s t r e a m istringstream istringstream进行流式计数

整体算法时间复杂度是 O ( n ) O(n) O(n), 但是边界情况很多, 实现起来比较复杂

代码

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cctype>using namespace std;class Solution {
public:vector<string> strs;void merge(vector<string> &ans) {int i = 0, n = strs.size();while (i < n) {string s = strs[i];i++;//对于当前字符串合并所有可能的字符串while (true) {if (strs.empty() || s.back() != '-' || i >= n) break;//去除连接符s.pop_back();string ne_s = strs[i];int pos = ne_s.find_first_not_of(' ');if (pos != string::npos) s += ne_s.substr(pos);else s += ne_s;i++;}ans.push_back(s);}}void handle(string &s, vector<string> &vec) {for (string &tmp : vec) {bool pre = true;for (char c : tmp) {if (isalpha(c)) {s += c;pre = false;}else if (!pre) {s += ' ';pre = true;}}if (!s.empty() && !pre) s += ' ';}}int GetWordsCnt(const vector<string> &lines) {strs = lines;vector<string> vec;merge(vec);string s;//执行步骤2handle(s, vec);istringstream is(s);string tmp;int ans = 0;while (is >> tmp) ans++;return ans;}
};

*后续 A C AC AC代码

#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <cctype>using namespace std;class Solution {
public:vector<string> strs;void merge(vector<string> &ans) {int i = 0, n = strs.size();while (i < n) {string s = strs[i];i++;while (true) {if (strs.empty() || s.back() != '-' || i >= n) break;s.pop_back();string &ne_s = strs[i];int pos = ne_s.find_first_not_of(' ');if (pos != string::npos) s += ne_s.substr(pos);else s += ne_s;i++;}ans.push_back(s);}}void handle(string &s, vector<string> &vec) {for (string &tmp : vec) {//记录前一个位置是否是空格bool pre = true;for (char c : tmp) {if (isalpha(c)) {s += c;pre = false;}//当前位置不是字母并且前一个位置不是空格说明是其他的分割符需要转化为空格并且标记当前位置是空格else if (!pre) {s += ' ';pre = true;}}//字符串不为空并且前面前面没有空格if (!s.empty() && !pre) s += ' ';}}int GetWordsCnt(const vector<string> &lines) {strs = lines;vector<string> vec;merge(vec);string s;handle(s, vec);istringstream is(s);string tmp;int ans = 0;while (is >> tmp) ans++;return ans;}
};
http://www.xdnf.cn/news/1742.html

相关文章:

  • 系统架构师2025年论文《系统架构风格2》
  • 生成运算树
  • AIP代码生成器——标准化接口开发智能工具
  • SpringMVC知识体系
  • 【MySQL数据库入门到精通-06 DCL操作】
  • 《数据结构之美--栈和队列》
  • 三格电子Profinet从站转EtherNet/IP从站网关:工业通信协议转换的桥梁
  • 每日Python 4.24
  • 动态自适应分区算法(DAPS)设计流程详解
  • 深度学习:迁移学习
  • 2025年04月24日Github流行趋势
  • 那些年开发踩过的坑
  • day002
  • C++/Qt中QActionGroup类用法
  • 100.HTB-Meow
  • Redis高级数据类型解析(二)——Set、Sorted Set与Geo实战指南
  • 怎么设定自动化测试目标?
  • AI打开潘多拉魔盒?当深度伪造成为虚假信息的核动力引擎
  • RAG 的完整流程是怎么样的?
  • 【扣子Coze 智能体案例四】五行八卦占卜智能体
  • ESP32_IDF_VScode安装多版本共存
  • MySQL-自定义函数
  • 济南国网数字化培训班学习笔记-第二组-2节-输电线路施工及质量
  • Spring MVC HandlerAdapter 的作用是什么? 为什么 DispatcherServlet 不直接调用 Controller 方法?
  • Redis Cluster 使用 CRC16 算法实现 Slot 槽位分片的核心细节
  • VocalPitchMonitor汉化版:专业音调检测,助力歌唱练习
  • 从零开始在Win上添加一块QEMU开发板(四)实现简单USART
  • Vue 2 的响应式 API 和 Vue 3 的组合式 API 的详细对比,从核心机制、使用方式、代码示例及优缺点展开
  • C++ 类与对象(上):从基础定义到内存布局的深度解析
  • PowerToys:让你的windows拥有更丝滑的体验