当前位置:网站首页>Application of the purple book p113map of ananagrams (uva156)
Application of the purple book p113map of ananagrams (uva156)
2022-06-28 20:36:00 【Love_ Jacques】
Purple Book P113;map Application ;UVa146 Ananagrams;
Vjudge Please click here for the title link
The main idea of the topic :
Enter some words ( The input contains several lines , No more than 80 Characters , It consists of some words . Words by no more than 20 Upper and lower case letters ), Find all the words that meet the following conditions : The word cannot be rearranged by letters , Get another word of the input text . When judging whether the conditions are met , Letters are not case sensitive , However, the input case should be kept when outputting , In dictionary order .
Sample Input
ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIed
noel dire Disk mace Rob dries
Sample Output
Disk
NotE
derail
drIed
eye
ladder
soon
Topic analysis ( The idea is based on the purple book ):
The question asks to find out “ No repetition ” 's words , So how to judge “ repeat ” Is the key point of this topic ;
Given in the title “ repeat ” Means : Two words with the same number of letters , There is no requirement for alphabetical order .
data structure choice : because map The property of having a value corresponding to a key , It can easily help us count the number of times each word appears , Easy to remove heavy , We can choose to build one map To store the number of occurrences of different words .
Algorithm design :map The rational use of can help us simplify the code , But we still need one that can be used efficiently map To achieve the choice of goals ; Since the title requires “ repeat ” There is no alphabetical order , Then we can put the order of the letters in the words Unification To facilitate comparison : Convert all letters to lowercase and sort them in dictionary order ;
void change(string& now)
{
for(int i=0;i<now.length();i++)
now[i]=tolower(now[i]);
sort(now.begin(),now.end());
return ;
}
Then compare the words according to the unified words ;
Module design : Definition and pretreatment – Input and initialization – Simulation judgment – Output –return 0;
Code :
#include<map>
#include<iostream>
#include<string>
#include<cctype>
#include<vector>
#include<algorithm>
using namespace std;
vector<string> txt;
map<string,int> words;
vector<string> answer;
string s;
//txt Save all the words you read ,words Save the initialized word and its occurrence times ,answer Save words that only appear once
void change(string& now);
int main()
{
while(cin>>s)
{
if(s=="#") break; // encounter # End reading
txt.push_back(s);
string now(s);
change(now); // initialization
words[now]++; // Count the times
}
for(int i=0;i<txt.size();i++)
{
string now(txt[i]);
change(now);
if(words[now]==1) answer.push_back(txt[i]);
}
sort(answer.begin(),answer.end()); // The output should be sorted in dictionary order
for(int i=0;i<answer.size();i++) cout<<answer[i]<<endl;
return 0;
}
void change(string& now)
{
for(int i=0;i<now.length();i++)
now[i]=tolower(now[i]);
sort(now.begin(),now.end());
return ;
}
Summary of key points and details :
- Flexible use map The correspondence between the middle key and the value , Choose according to the scene ;
- sort Two cases of use <1.
sort(a,a+n)<2.sort(m.begin(),m.end());
Updated on 2020.6.24
边栏推荐
猜你喜欢

应用实践 | 10 亿数据秒级关联,货拉拉基于 Apache Doris 的 OLAP 体系演进(附 PPT 下载)

Automatic operation and maintenance platform based on Apache APIs

rsync远程同步

2022焊工(初级)特种作业证考试题库及答案

Analysis of all knowledge points of TCP protocol in network planning

28 rounds of interviews with 10 companies in two and a half years

Data standardization processing

Bluecmsv1.6 code audit

我也差点“跑路”

学习太极创客 — MQTT 第二章(八)ESP8266 MQTT 用户密码认证
随机推荐
Ehcache配置资料,方便自己查
软件watchdog和ANR触发memory dump讲解
risc-v指令集
稳定性总结
mysql-发生系统错误1067
大智慧上怎么进行开户啊, 安全吗
On the complexity of software development and the way to improve its efficiency
iterator中的next()为什么要强转?
Learning Tai Chi Maker - mqtt Chapter II (VII) esp8266 mqtt Testament application
T检验(检验两个总体的均值差异是否显著)
TcWind 模式設定
ANR无响应介绍
阿里云 MSE 基于 Apache APISIX 的全链路灰度方案实践
1. integrate servlets
酷学院华少:如何在SaaS赛道里做成一家头部公司
1. 整合 Servlet
Relevant calculation of sphere, etc
Tcwind mode setting
Day88. qiniu cloud: upload house source pictures and user avatars
03.hello_ rust