统计不同的单词(map应用)
题目描述:
输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词。在判断是否满足条件时,字母不区分大小写,但在输出时应保留输入中的大小写,按字典序进行排列(所有大写字母在所有的小写字母的前面)。
#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<cctype>
#include<map>
using namespace std;
map<string,int> cnt;
vector<string> words;
//对单词进行"标准化"
string repr(const string& s)
{
string cs=s;
for(int i=;i<cs.length();i++)
{
cs[i]=tolower(cs[i]);
}
sort(cs.begin(),cs.end());
return cs;
}
int main()
{
string s;
while(cin>>s)
{
if(s[]=='#') break;
words.push_back(s);
string cs=repr(s);
if(!cnt.count(cs)) cnt[cs]=;
cnt[cs]++;
}
vector<string> ans;
for(int i=;i<words.size();i++)
{
if(cnt[repr(words[i])]==) ans.push_back(words[i]);//若想不到"标准化",则发挥不到map的效果
}
sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++)
{
cout<<ans[i]<<endl;
}
return ;
}
统计不同的单词(map应用)的更多相关文章
- PHP统计字符串里单词查询关键字
<?function full_count_words($str) { //返回完整数组,包含字符串里每个单词 $words = str_word_count($str,1); ...
- c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图
Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...
- java统计文本中单词出现的个数
package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...
- C 循环统计输入的单词个数和字符长度
C 循环统计输入的单词个数和字符长度 #include <stdio.h> #include <Windows.h> int main(void) { ]; ; ; print ...
- JAVA实验--统计文章中单词的个数并排序
分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...
- Spark——统计文本中单词出现的次数
示例一:统计所有单词出现的次数 1.在本地创建文件并上传到hdfs中 #vin data.txt //将文件上传到hadoop的根目录下 #hdfs dfs -put data.txt / 2.在sp ...
- Python 基础 - 统计文本里单词的个数以及出现的次数
# -*- coding:utf-8 -*- #author:V def tol (file1,gui): #写一个方法,定义文件,or 匹配规则 import re patt = re.compil ...
- shell统计文本中单词的出现次数
Ubuntu14.04 给定一个文本,统计其中单词出现的次数 方法1 # solution 1 grep与awk配合使用,写成一个sh脚本 fre.sh sh fre.sh wordfretest.t ...
- 统计文件中单词的个数---Shell及python版
最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash ];then echo "Usage:basename $0 filename" ...
随机推荐
- TensorFlow_action
安装TensorFlow 包依赖 C:\Users\sas> pip3 install --upgrade tensorflow Collecting tensorflow Downloadi ...
- php自定义函数: 计算两个时间日期相隔的天数,时,分,秒
function timediff( $begin_time, $end_time ) { if ( $begin_time < $end_time ) { $starttime = $begi ...
- MySQL存储过程-通过数据库里已存在的IP查询城市
CREATE DEFINER=`mazey`@`%` PROCEDURE `sp_find_city_by_ip`(IN `in_visitor_ip` varchar(20),OUT `out_vi ...
- JDK动态代理连接池
JDK动态代理 1 什么是JDK动态代理 刚刚写ItcastConnection时爽么?因为Connection中的方法太多了,每个都要写,所以很累吧.累点到是没什么,可以完成功能就是好的.但是不 ...
- - symfony/icu v1.2.0 requires lib-icu >=4.4 -> the requested linked library icu has the wrong version installed or is missing from your system, ma
$ composer install Loading composer repositories with package information Installing dependencies (i ...
- (转)ubuntu 12.04搭建Adobe Flash Media Server服务
破解版传送门:http://fms45.cuplayer.com/fms4download.html 福利:1462-5247-1705-7678-8379-5590 下载解压 cd进目录,./ins ...
- ARDUINO W5100 WebServer测试
1.直接下载官方的enternet->WebServer代码 /* Web Server A simple web server that shows the value of the anal ...
- mathjax
MathJax.Hub.Typeset() method. This will cause the preprocessors (if any were loaded) to run over the ...
- 第三章 python中的字符串
一.字符串的基本操作 所有标准的序列操作对字符串同样适用,如索引.分片.乘法.判断成员是否存在.求长度.最大值和最小值等.记住一点,字符串是不可变的. 二.字符串中重要的方法 1.find(subst ...
- react项目中antd组件库的使用需要注意的问题
antd是蚂蚁金服推出的ui组件库,给我们在react项目开发中提供了大大的便利.但在使用的过程中,或多或少的会遇到一些问题,毕竟,用的是别人的东西,就得遵守别人的规则嘛!官方文档:https://a ...