A Queen’s graduate student is writing her thesis in Computer Science. She’s very worried that her advisor will be displeased because she isn’t using enough big words. Being the cunning computer scientist that she is, she decides to write an application that will count the length of her words.
    Words can be separated by blanks, question marks, exclaimation marks, commas and periods (the punctuation is not to add to the length of the word). Words with apostrophes, such as “I’m” and “You’ve”, and hyphenated words are treated as one word. For example, “you’ve” is a 5-letter word. Hyphenation may occur across two lines.
Input
Input file contains several blocks of input. Each block is terminated by a line containing a # as its first character. You should not consider this line within the block. There is no space at the end of a line. You may assume each word has no more than 30 characters, and each line has no more than 80 characters. You may assume the entire text of a block will not end with a hyphen.
Output
The output for each block will consist of two columns of numbers. The first column will represent the length of the word (They appear in ascending order) and the second column will represent the frequency of that length of word. The output should not have any word length that has no word in the input. Print a blank line after the output for each block of input.
Sample Input
This is funny!
Mr.P and I’ve never seen
this ice-cream flavour
before.Crazy eh?

This is funny!
Mr.P and I’ve never seen
this ice-cream flavour
before.Crazy eh?

Sample Output
1 1
2 3
3 2
4 3
5 3
6 1
7 1
8 1

1 1
2 3
3 2
4 3
5 3
6 1
7 1
8 1

问题链接:UVA10293 Word Length and Frequency
问题简述:(略)
问题分析
    单词长度频度计算问题。
    简单题,不解释。先来一版用函数strtok()实现的,也许按字符流处理更为合理。
    虽然读入的字符串(按空格、制表符和换行分割)放在字符数组中,也可以将其看作一个字符流进行处理。前一个程序按字符流进行处理。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10293 Word Length and Frequency */#include <bits/stdc++.h>using namespace std;const int N = 30;
char word[N + 1];
int lencnt[N + 1];int isSeparator(char c)
{return c == ' ' || c == '.' || c == ',' || c == '?' || c == '!';
}int main()
{memset(lencnt, 0, sizeof(lencnt));while(~scanf("%s", word)) {if(strcmp(word, "#") == 0) {for(int i = 1; i <= N; i++)if(lencnt[i]) printf("%d %d\n", i, lencnt[i]);printf("\n");memset(lencnt, 0, sizeof(lencnt));} else {int wlen = strlen(word);if(word[wlen - 1] == '-') {      // 处理单词续行的情况scanf("%s", word + wlen - 1);wlen = strlen(word);}int len = 0;for(int i = 0; i < wlen; i++) {if(isalpha(word[i])) len++;else if(isSeparator(word[i])) {     // 用标点符号切割单词lencnt[len]++;len = 0;}       // 其他字符,例如“'”,不算长度}lencnt[len]++;}}return 0;
}

AC的C++语言程序(使用函数strtok())如下:

/* UVA10293 Word Length and Frequency */#include <bits/stdc++.h>using namespace std;const int N = 30;
char word[N + 1];
int lencnt[N + 1];int main()
{memset(lencnt, 0, sizeof(lencnt));while(~scanf("%s", word)) {if(strcmp(word, "#") == 0) {for(int i = 1; i <= N; i++)if(lencnt[i]) printf("%d %d\n", i, lencnt[i]);printf("\n");memset(lencnt, 0, sizeof(lencnt));} else {int len = strlen(word);if(word[len - 1] == '-') {      // 处理单词续行的情况scanf("%s", word + len - 1);len = strlen(word);}char *p = strtok(word, ".,?!");     // 用标点符号切割单词while(p != NULL) {len = strlen(p);for(int i = 0; i < len; i++)if(!isalpha(p[i]))len--;lencnt[len]++;p = strtok(NULL, ".,?!");}}}return 0;
}

UVA10293 Word Length and Frequency【单词长度频度+strtok】相关推荐

  1. leetcode 318. Maximum Product of Word Lengths | 318. 最大单词长度乘积

    题目 https://leetcode.com/problems/maximum-product-of-word-lengths/ 题解 中规中矩的题目,中规中矩的思路,不像是一个 medium 题. ...

  2. 408. Valid Word Abbreviation有效的单词缩写

    [抄题]: Given a non-empty string s and an abbreviation abbr, return whether the string matches with th ...

  3. LeetCode 391. 完美矩形(扫描线) / 318. 最大单词长度乘积 / 563. 二叉树的坡度

    391. 完美矩形 2021.11.16 每日一题 题目描述 给你一个数组 rectangles ,其中 rectangles[i] = [xi, yi, ai, bi] 表示一个坐标轴平行的矩形.这 ...

  4. JAVA(2021-11-17)leetcode每日一题---- 最大单词长度乘积

    这个题的难点并不在于找的过程,因为没有什么可以记录的,就是要挨个比,n平方的复杂度.但是这个题的优化空间在于比较两个单词是否含有公共字母. 一看到是否含有重复的,是否含有重复字符,我们就应该想到是不是 ...

  5. 跟我打卡LeetCode 58最后一个单词长度59螺旋矩阵Ⅱ60排列序列

    原创公众号:bigsai 关注后回复进群即可加入力扣打卡群,欢迎划水.近期打卡: LeetCode 47全排列Ⅱ&48旋转图像 LeetCode 49字母异位词分组&50pow(x,n ...

  6. 第6周 使用对象 1 单词长度(5分) 2 GPS数据处理(5分)

    第6周 使用对象 By yangbo 2021/04/18 6.1 字符类型 字符也是Java中基础的数据类型之一,Java采用Unicode16表达字符,在所有的机器上,不管CPU.操作系统和本地语 ...

  7. 【天梯赛】单词长度、社交网络图中结点的“重要性”计算、朋友圈、家谱处理、狼人杀

    1.单词长度 单词长度 #include<bits/stdc++.h> using namespace std; const int N=2e8+10; string s; int fla ...

  8. 散列表(上):Word 文档中单词拼写检查功能是如何实现的?

    本文是学习算法的笔记,<数据结构与算法之美>,极客时间的课程 在平时我们使用Word的时候,输入一个错误的单词,它就会用标红的方式提示"拼写错误".Word的这个单词拼 ...

  9. 7-26 单词长度 (C语言)

    7-26 单词长度 (15 分)你的程序要读入一行文本,其中以空格分隔为若干个单词,以.结束.你要输出每个单词的长度.这里的单词与语言无关,可以包括各种符号,比如it's算一个单词,长度为4.注意,行 ...

最新文章

  1. Android架构篇-2 国际化多语言
  2. iOS实现动态区域裁剪图片
  3. 设计模式解析(五)——几种设计模式之Facade和Adapter
  4. 30 张图解 | 高频面试知识点总结:面试官问我高并发服务模型哪家强?
  5. libstdc++.so, needed by ../../../rknn_api/arm64-v8a/librknn_api.so, not found
  6. secureCRT 右键的设置选中就copy or 选中即粘贴
  7. 时间同步绝对是一个大问题
  8. 人人都能看懂的 6 种限流实现方案!
  9. 源码分析参考:Scheduler
  10. (六)构建MLOps模型API
  11. 中山大学Delphi视频教程 共51课
  12. 解决nginx服务器显示txt文本为乱码的问题
  13. pdf转cad转换器转换快速简单方法
  14. 玉品游戏java_整蛊游戏N合一(玉品)
  15. MATLAB中clc命令详解
  16. 基于单片机的RFID刷卡门禁电路设计(#0206)
  17. 永久修改服务器bmc主机名,如何在 VMM 中配置主机 BMC 设置
  18. 医疗器械软件测试相关
  19. 用于长延迟多径衰落环境下的强化UF-OFDM
  20. 制造业如何数字化转型?

热门文章

  1. ceres-solver库编译说明
  2. ArcGIS制图表达Representation实战篇1-边界线和行道树制作
  3. SpringCloud系列之Nacos+Dubbo+Seata应用篇
  4. SpringBoot开发流程
  5. 12个免费的 Twitter Bootstrap 后台模板
  6. 常用JQuery插件整理 1
  7. WebKit新特性WebGL
  8. 高级着色语言HLSL入门(6)
  9. chrome jsp 显示不正常_selenium+java谷歌浏览器 网站打开不正常
  10. 【java学习之路】(java框架)002.Git配置及使用