#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include<set>
using namespace std;
// 函数:判断是否为唯一可译码
set <string> s;
bool y = true;
bool cheak(string f, int ch, std::vector<std::string> c, int n) {
if (y == false) {
return y;
}
string a, b;
for (int i = 0; i < n; i++) {
if (c[i] == f && (ch < 0 || (ch >= 0 && ch != i))) {
y = false;
return y;
}
else if (c[i] == f && ch == i) {
continue;
}
else if (c[i].length() > f.length()) {
a = c[i];
b = f;
}
else {
b = c[i];
a = f;
}
a.erase(0, b.length());
if (s.find(a) == s.end() && a.length() != 0) {
//cout << a << " ";
s.insert(a);
cheak(a, -1, c, n);
}
}
return y;
}
bool isUniqueDecodable(std::vector<std::string> codes) {
bool a = true;
int n = codes.size();
for (int i = 0; i < n; i++) {
if (y == false)
{
break;
}
cheak(codes[i], i, codes, n);
}
return y;
}
int main() {
std::ifstream fin("in3.txt");
std::ofstream fout("out3.txt");
std::vector<std::string> codes;
std::string code;
if (!fin.is_open() || !fout.is_open()) {
std::cerr << "没有这个文件!" << std::endl;
return 1;
}
int cnt = 0;
while (fin >> code) {
if (code == "$") {
// 处理一组码
std::string ans = "第" + std::to_string(++cnt) + "组";
fout << (isUniqueDecodable(codes) ? ans + "是" : ans + "不是") << std::endl;
s.clear();
y = true;
codes.clear(); // 清空codes向量以处理下一组码
}
else {
codes.push_back(code);
}
}
fin.close();
fout.close();
return 0;
}
判断是否为唯一可译码(c++)
原创
©著作权归作者所有:来自51CTO博客作者WonderKing66的原创作品,请联系作者获取转载授权,否则将追究法律责任
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
C++判断字符串是否为回文
C++判断字符串是否为回文!
字符串 #include ios 其他 -
Windows判断是否为64位程序(C++)
思路:1.先判断是否为64位系统2.判断是否为64位程序代码:// ProcessType.cpp : 此文件包含 "mai
Windows 64位系统 #include -
C++判断字符串是否为数字
机器学习以及人工智能的学习需要扎实的数学功底才能走的更远,爬的更高,所以打
人工智能 机器学习 #include -
C++判断vector是否递增
bool isUp(vector<int>&vec){ for(int i=1;i<vec.size();i++)
c++ leetcode 开发语言 i++ -
c 判断是否为nan
输出
#include 分享