【习题 4-6 UVA - 508】Morse Mismatches
【链接】 我是链接,点我呀:)
【题意】
给你每个字母对应的摩斯密码。
然后每个单词的莫斯密码由其组成字母的莫斯密码连接而成。
现在给你若干个莫斯密码。
请问你每个莫斯密码对应哪个单词。
如果有多个单词和他对应。那么输出字典序最小的那个。
如果没有单词和他对应。
那么,你可以删除或者添加若干字母(只能一直删或一直增加)
问你在这种情况下能匹配到的单词(要求添加或者删的单词的数量最小)
如果有多种可能。输出字典序最小的。
如果一直删除。一直增加也找不到。
那么输出所有单词里面字典序最小的哪个。
【题解】
模拟就好。
先对字典排个序再模拟。
【代码】
#include <bits/stdc++.h>
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
using namespace std;
const int N = 1e3;
int n;
string s;
string dic[300];
vector<pair<string,string> > v;
string _find(string s){
int step = 0;string ans = "";
rep1(i,0,(int)v.size()-1){
if (v[i].second==s) {
if (step==0) ans = v[i].first;
step++;
}
}
if (step>0){
if (step>1) ans+="!";
return ans;
}
step = -1;
int len1 = s.size();
rep1(i,0,(int)v.size()-1){
int len2 = v[i].second.size();
if (len2<len1){
string temp = s.substr(0,len2);
if (temp==v[i].second){
if (step==-1){
step = len1-len2;
ans = v[i].first;
}else if (len1-len2<step){
step = len1-len2;
ans = v[i].first;
}
}
}else{
//len2>=len1
string temp = v[i].second.substr(0,len1);
if (temp==s){
if (step==-1){
step = len2-len1;
ans = v[i].first;
}else if (len2-len1<step){
step = len2-len1;
ans = v[i].first;
}
}
}
}
if (step==-1) ans = v[0].first;
ans+="?";
return ans;
}
int main(){
//freopen("/home/ccy/rush.txt","r",stdin);
// freopen("/home/ccy/rush_out.txt","w",stdout);
ios::sync_with_stdio(0),cin.tie(0);
while (cin >> s){
if (s[0]=='*') break;
string cor;
cin >> cor;
dic[s[0]] = cor;
}
while (cin >> s){
if (s[0]=='*') break;
string temp = "";
rep1(i,0,(int)s.size()-1){
temp += dic[s[i]];
}
v.push_back({s,temp});
}
sort(v.begin(),v.end());
while (cin >> s){
if (s[0]=='*') break;
cout<<_find(s)<<endl;
}
return 0;
}
【习题 4-6 UVA - 508】Morse Mismatches的更多相关文章
- uva 508 - Morse Mismatches(摩斯码)
来自https://blog.csdn.net/su_cicada/article/details/80084529 习题4-6 莫尔斯电码(Morse Mismatches, ACM/ICPC Wo ...
- uva 508 Morse Mismatches
Samuel F. B. Morse is best known for the coding scheme that carries his name. Morse code is still us ...
- UVA 508 Morse Mismatches JAVA
题意:输入字母和数字的编码,输入词典,输入一段编码,求出对应的单词. 思路:来自https://blog.csdn.net/qq_41163933/article/details/82224703 i ...
- UVa 508 Morse Mismatches (模糊暴力)
题意:莫尔斯电码,输入若干个字母的Morse编号,一个字典和若干编码.对于每个编号,判断它可能的是哪个单词, 如果有多个单词精确匹配,输出第一个单词并加一个“!”:如果无法精确匹配,那么在编码尾部增加 ...
- [刷题]算法竞赛入门经典(第2版) 4-6/UVa508 - Morse Mismatches
书上具体所有题目:http://pan.baidu.com/s/1hssH0KO 代码:(Accepted,10 ms) //UVa508 - Morse Mismatches #include< ...
- ACM训练计划建议(写给本校acmer,欢迎围观和指正)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
- ACM训练计划建议(转)
ACM训练计划建议 From:freecode# Date:2015/5/20 前言: 老师要我们整理一份训练计划给下一届的学弟学妹们,整理出来了,费了不少笔墨,就也将它放到博客园上供大家参考. 菜 ...
- 动态规划 Dynamic Programming 学习笔记
文章以 CC-BY-SA 方式共享,此说明高于本站内其他说明. 本文尚未完工,但内容足够丰富,故提前发布. 内容包含大量 \(\LaTeX\) 公式,渲染可能需要一些时间,请耐心等待渲染(约 5s). ...
- UVa第五章STL应用 习题((解题报告))具体!
例题5--9 数据库 Database UVa 1592 #include<iostream> #include<stdio.h> #include<string.h&g ...
随机推荐
- hive-0.11.0安装方法具体解释
先决条件: 1)java环境,须要安装java1.6以上版本号 2)hadoop环境,Hadoop-1.2.1的安装方法參考hadoop-1.2.1安装方法具体解释 本文採用的hado ...
- ASP.NET MVC中的嵌套布局页
在WEB窗体模式中,用惯了母版页,并且常有母版页嵌套的情况. 而在MVC模式下,对应母版页的,称作为布局页.默认的布局页为 ~/Views/Shared/_Layout.cshtml.默认每个页面都会 ...
- 阿里云 Docker-registry 搭建
阿里云 仓库地址: https://cr.console.aliyun.com/cn-hangzhou/instances/images
- Linux下使用popen()执行shell命令【转】
本文转载自:https://my.oschina.net/u/727148/blog/262987 函数原型: #include “stdio.h” FILE popen( const char co ...
- SpringMVC使用POST方法传递数据,却出现Request method 'GET' not supported?
转自:https://segmentfault.com/q/1010000011245770 问题:没有使用get获取当前页面解决方案: @RequestMapping(value = " ...
- 快速掌握C#7
1. out 变量(out variables) 以前我们使用out变量必须在使用前进行声明,C# 7.0 给我们提供了一种更简洁的语法 “使用时进行内联声明” .如下所示: var input = ...
- [转]利用 NPOI 變更字體尺寸及樣式
本文转自:http://blog.cscworm.net/?p=1650 利用 NPOI 變更字體尺寸及樣式: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ...
- BZOJ 3930 容斥原理
思路: 移至iwtwiioi http://www.cnblogs.com/iwtwiioi/p/4986316.html //By SiriusRen #include <cstdio& ...
- BZOJ 4753 二分+树形DP
思路: 先二分答案 f[x][j]表示在x的子树里选j个点 f[x][j+k]=max(f[x][j+k],f[x][j]+f[v[i]][k]); 初始化 x!=0 -> f[x][1]=p[ ...
- 自学Python九 爬虫实战二(美图福利)
作为一个新世纪有思想有文化有道德时刻准备着的屌丝男青年,在现在这样一个社会中,心疼我大慢播抵制大百度的前提下,没事儿上上网逛逛YY看看斗鱼翻翻美女图片那是必不可少的,可是美图虽多翻页费劲!今天我们就搞 ...