国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a"
对应 ".-"
, "b"
对应 "-..."
, "c"
对应 "-.-."
, 等等。
为了方便,所有26个英文字母对应摩尔斯密码表如下:
[".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."]
给定一个单词列表,每个单词可以写成每个字母对应摩尔斯密码的组合。例如,"cab" 可以写成 "-.-.-....-",(即 "-.-." + "-..." + ".-"字符串的结合)。我们将这样一个连接过程称作单词翻译。
返回我们可以获得所有词不同单词翻译的数量。
例如:
输入: words = ["gin", "zen", "gig", "msg"]
输出: 2
解释:
各单词翻译如下:
"gin" -> "--...-."
"zen" -> "--...-."
"gig" -> "--...--."
"msg" -> "--...--."
共有 2 种不同翻译, "--...-." 和 "--...--.".
注意:
- 单词列表
words
的长度不会超过100
。 - 每个单词
words[i]
的长度范围为[1, 12]
。 - 每个单词
words[i]
只包含小写字母。
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> d = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
unordered_set<string> s;
for (auto word : words) {
string code;
for (auto c : word) code += d[c - 'a'];
s.insert(code);
}
return s.size();
}
uniqueMorseRepresentations(vector<string>& words) {
vector<string> d = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
unordered_set<string> s;
for (auto word : words) {
string code;
for (auto c : word) code += d[c - 'a'];
s.insert(code);
}
return s.size();
}
用到了unordered_set的两个功能 s.insert(),s.size()
//查找函数 find() 通过给定主键查找元素
unordered_set<int>::iterator find_iter = c1.find(1);
//value出现的次数 count() 返回匹配给定主键的元素的个数
c1.count(1);
//返回元素在哪个区域equal_range() 返回值匹配给定搜索值的元素组成的范围
pair<unordered_set<int>::iterator, unordered_set<int>::iterator> pair_equal_range = c1.equal_range(1);
//插入函数 emplace()
c1.emplace(1);
//插入函数 emplace_hint() 使用迭代器
c1.emplace_hint(ite_begin, 1);
//插入函数 insert()
c1.insert(1);
//删除 erase()
c1.erase(1);//1.迭代器 value 区域
//清空 clear()
c1.clear();
//交换 swap()
c1.swap(c2);
uniqueMorseRepresentations(vector<string>& words)
&在C语言中的用法
(a) 逻辑与:if((a>1)&&(b<0))
(b) 位运算与:x=a&b;
(c) 逻辑与赋值:x&=y;与 x=x&y含义相同
(d) 求地址运算符:p=&x;读法:把x的地址赋给p(指针)
C++中&的补充用法
C++中有一种C不存在的变量类型引用变量(简单说来为引用),尽管在C语言中用指针也可以实现类似的功能。
引用,指针,地址是联系密切的概念。地址是在电脑内存中的地址(一般是一些变量的值在内存中的储存位置),指针是存地址的变量,所以指针可以“指向”内存地址。概念上讲,引用变量本质上是指针的另一个名字(但是并不能被编译器实例化)
在函数内像其他变量一样定义一个引用是可能的
引用的使用和对引用的变量的使用是一样的
void main(void)
{
int i=3;
f(i);
cout << i;
}
void f(int& r)
{
r = 2*r;
}
程序输出“6”
在C语言中,实现同样的功能,我们可以通过声明f()为void f(int *r),其中r是指向整数类型的指针,然后调用参数&i(i的地址)调用函数f(),在函数f()内使用r的解引用,但是显然,C++提供了一种更简明的通过引用的方式向函数传值,从函数中返回值。
例1:int a; int &ra=a; //定义引用ra,它是变量a的引用,即别名
例2:引用传递能够改变实参的值,即便没有返回值。
例3:常引用声明方式:const 类型标识符 &引用名=目标变量名;
用这种方式声明的引用,不能通过引用对目标变量的值进行修改,从而使引用的目标成为const,达到了引用的安全性。
int a ;
const int &ra=a;
ra=1; //错误
a=1; //正确
____________________________________________________最终结果__________________________________________________
#include "stdafx.h"
#include <unordered_set>
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
using namespace std;
int uniqueMorseRepresentations(vector<string>& words) {
vector<string> d = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
unordered_set<string> s;
for (auto word : words) {
string code;
for (auto c : word) code += d[c - 'a'];
s.insert(code);
}
return s.size();
}
int main()
{
vector<string> words= { "gin", "zen", "gig", "msg" };
int res = 0;
res = uniqueMorseRepresentations(words);
cout << res << endl;
system("pause");
return 0;
}