统计不同的单词(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" ...
随机推荐
- On the importance of initialization and momentum in deep learning
Ilya Sutskever1 ilyasu@google.com James Martens jmartens@cs.toronto.edu George Dahl gdahl@cs.toronto ...
- Activiti使用过程_1
1 微信公众号:
- #!/usr/bin/python和#!/usr/bin/env 的区别(转)
#!/usr/bin/python和#!/usr/bin/env 的区别 #!/usr/bin/python 通常在一个.py文件开头都会有这个语句 它只在Linux系统下生效,意思是当作为可执行 ...
- mysql设计表时出错
source下面那个字段没有设置类型,类型为空
- 牛客小白月赛1 G あなたの蛙は旅⽴っています【DP】
题目链接 https://www.nowcoder.com/acm/contest/85/G 思路 按照题解上的方式 存取数据 然后DP一下 就可以了 AC代码 #include <cstdio ...
- 【LeetCode】【找元素】Find First and Last Position of Element in Sorted Array
描述: Given an array of integers nums sorted in ascending order, find the starting and ending position ...
- c++之helloworld与命名空间
首先在linux中需要安装g++编译器. 在中端输入 uname -a,可以查看版本信息. 输入g++,如果提示错误.则需要使用sudo apt-get install g++. #include&l ...
- 第三篇、dom操作续
一.属性操作 属性操作 attributes // 获取所有标签属性 setAttribute(key,value) // 设置标签属性 getAttribute(key) // 获取指定标签属性 r ...
- poj 3083 Children of the Candy Corn 【条件约束dfs搜索 + bfs搜索】【复习搜索题目一定要看这道题目】
题目地址:http://poj.org/problem?id=3083 Sample Input 2 8 8 ######## #......# #.####.# #.####.# #.####.# ...
- Spring Cloud之Zuul网关集群
Nginx+Zuul 一主一备 或者 轮训多个 在微服务中,所有服务请求都会统一到Zuul网关上. Nginx 配置: #user nobody; worker_processes 1; #error ...