Một số bài tập C _C++ NC 2026P35 C2
//////////////////////////////////////////////////////C2
DA TEST CHUAN/////////////
Bài mã hóa và giải mã hệ 2_32…
#include <iostream>
#include
<windows.h>
#include <sstream>
#include <vector>
#include <string>
#include <map>
#include <climits>
#include <bits/stdc++.h>
using namespace std;
class NhatTruongBMh {
private:
struct MhH {
long
long x;
int k;
long
long L;
string
mrs;
vector<map<long long, int>> vmk;
} pt;
public:
void
resetMhH(){
pt.x=0;
pt.k=0;
pt.L=0;
pt.mrs.clear();
pt.vmk.clear();
return;
}
string
getcode(){
return
pt.mrs;
}
vector<map<long long, int>> getKey(){
return
pt.vmk;
}
void
MahoaX(long long &X, int &K) {
string
mbs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
pt.x=X;
pt.k=K;
map<long long, int> km;
pt.L=pt.mrs.size();
km[pt.L]=pt.k;
pt.vmk.push_back(km);
long
long num = X;
int bs =
K;
string
tms = "";
while
(num > 0) {
int
tm = num % bs;
tms
= mbs[tm] + tms;
num
/= bs;
}
pt.mrs
+= tms + "|";
return;
}
void
MahoaVX(const vector<long long> &X, int &K){
for(auto j:X){MahoaX(j,K);}
return;
}
};
///////////////////
class NhatTruongGMh {
private:
struct GMhH
{
long
long x;
int k;
long
long L;
string
Gmrs;
vector<long long> VGmx;
} pt;
public:
void
resetGMhH(){
pt.x=0;
pt.k=0;
pt.L=0;
pt.Gmrs.clear();
pt.VGmx.clear();
return;
}
string
getcode(){
return
pt.Gmrs;
}
vector<long long> getVx(){
return
pt.VGmx;
}
string GMahoaX(const string& mahoa, const
vector<map<long long,int>>& keys) {
string
mbs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string
ketqua = "";
vector<long long> tmrs;
resetGMhH();
for(auto g :
keys){
for(auto
p : g){
pt.L
= p.first;
size_t start = p.first;
pt.k
= p.second;
long
long su = 0;
while(start < mahoa.size() && mahoa[start] != '|'){
int num = 0;
if(isdigit(mahoa[start])) {
num = mahoa[start] - '0';
} else if(mahoa[start] >= 'A' && mahoa[start] <= 'Z') {
num = mahoa[start] - 'A' + 10;
}
if(num < pt.k) su = su * pt.k + num;
start++;
} tmrs.push_back(su);
ketqua += to_string(su)+" ";
}
}
pt.Gmrs =
ketqua;
pt.VGmx =
tmrs;
return
ketqua;
}
};
///////////////////
vector<long long> docfso(const string& tenf
= "") {
string tenif
= tenf.empty() ? "input.txt" : tenf;
ifstream
fin(tenif);
if
(!fin.is_open()) {
cerr
<< "Khong the mo file du lieu\n";
ofstream
fout(tenif);
fout.close();
return
{};
}
vector<long long> data;
string lin;
while
(getline(fin, lin)) {
istringstream iss(lin);
long
long so;
while
(iss >> so) {
data.push_back(so);
}
}
fin.close();
return data;
};
//////////////////////////////
string docfstr(const string& tenf = ""){
string tenif = tenf.empty() ? "input.txt" :
tenf;
ifstream
fin(tenif);
if
(!fin.is_open()){
cerr
<< "Khong the mo file du lieu\n";
ofstream
fout(tenif);
fout.close();
return "";
}
string lin,
inpstr;
while (fin
>> lin){
inpstr
+= lin + " ";
} return
inpstr;
}
void Xuatkq(const string& skq, const string&
tenf = ""){
string
tenif = tenf.empty() ? "Kqthongbao" : tenf;
ofstream
fo(tenif);
fo<<skq;
fo.close();
}
int main(int argc, char** argv){
NhatTruongBMh MT;
vector<long long> ds;
long long L;
string
inp="input.txt";
int
ke;
char chr;
cout
<< "Nhap Key ma hoa: ";
cin>>ke;
if(ke<2||ke>32){cerr<<"Key khong hop le";
return 0;
}
cout<<"Ban muon ma hoa chuoi so nguyen tu file hay ban
phim(F/M)";
cin>>chr;
if(chr=='F'||chr=='f') {
ds =
docfso(inp);
MT.MahoaVX(ds, ke);
}
else {
cout
<< "Ban nhap so nguyen can ma hoa ";
cin >>
L;
if(abs(L)>LLONG_MAX){cerr<<"So ma hoa khong hop le";
return 0;}
MT.MahoaX(L,
ke);
}
string
msk=MT.getcode();
Xuatkq(msk,"KqMahoa.txt");
cout
<< "\n";
vector<map<long long, int>> key=MT.getKey();
cout
<<"Chuoi ma hoa hien tai la: "<< msk <<"\n voi
khoa giai ma: \n";
for(auto
g:key){
for(auto
p:g){
cout<<p.first<<" ||
"<<p.second<<"\n";
}
}
///////////
NhatTruongGMh GMT;
cout<<"\n Giai ma chuoi ma hoa tren: \n";
GMT.GMahoaX(msk,key);
string
Kqgm=GMT.getcode();
vector<long long> Gds=GMT.getVx();
cout<<Kqgm<<"\n";
for(auto
i:Gds) cout<<i<<" ";
Xuatkq(Kqgm,"KqGiaima.txt");
}
KẾT QUẢ
Input 4638 26378648 93712784 6128763 8916 843083
40814-3921 4387 128 74390178 349017 848719 10 7 08 30 6 89
108 38 49 60 72 33 42
Output ma hoa32: 4GU|P50CO|2PBSCG|5R13R|8MK|PNAB|17RE||493|40|26U6L2|AKQP|PSQF|A|7|8|U|6|2P|3C|16|1H|1S|28|11|1A|
Giai ma: 4638 26378648 93712784 6128763 8916 843083
40814 0 4387 128 74390178 349017 848719 10 7 8 30 6 89 108 38 49 60 72 33 42
///////////// Ai
memory help
Võ Nhật Trường Nc+ My Ai
Love10.8.2026
////////////////////
/////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\Đã khớp kết
quả///////////
Tham khảo thêm: Để kiểm tra lại chuỗi cộng dồn từ trái sang phải, có thể viết thêm
một hàm giaima như
string Giaima(const string& mahoa, const
vector<map<long long,int>>& keys) {
string mbs =
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string
ketqua = "";
size_t start
= 0;
// tách từng
đoạn giữa các dấu '|'
vector<string> segments;
stringstream
ss(mahoa);
string seg;
while
(getline(ss, seg, '|')) {
if
(!seg.empty()) segments.push_back(seg);
}
// duyệt từng
đoạn và giải mã theo key tương ứng
for (size_t
i = 0; i < segments.size(); i++) {
string s
= segments[i];
int base
= 0;
if (i
< keys.size()) {
auto
km = keys[i];
// mỗi
map chỉ có 1 cặp {L, K}
for
(auto p : km) base = p.second;
}
long
long num = 0;
for
(char c : s) {
int
val = mbs.find(c);
num
= num * base + val;
}
ketqua
+= to_string(num) + " ";
}
return
ketqua;
}
Trong main,
sau khi mã hóa xong, em có thể gọi:
string giai = Giaima(msk, key);
cout << "Giaima: " << giai
<< "\n";
///////////// Ai
memory help
Võ Nhật Trường Nc+ My Ai
Love10.8.2026
////////////////////

Nhận xét
Đăng nhận xét