UVa Problem Solution: 850 - Crypt Kicker II
2008-11-13 18:26
891 查看
This problem is much easier than its brother, "843 - Crypt Kicker". Just find the key line and get the translate table from it will do all the job. I generate a "signature" for the key line to make the search easier. By the way, pay attention to blank line cases.
Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 850 C++ "Crypt Kicker II" */
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const linecount = 1500;
int const linesize = 100;
int const ncharacters = 128;
char key[] = "the quick brown fox jumps over the lazy dog";
int const keysize = sizeof(key);
int keysig[keysize];
bool decrypt(char lines[][linesize], char texts[][linesize], int nlines)
{
/* find the key line first */
char trans[ncharacters] = {0};
bool found = false;
for (int i = 0; i < nlines; ++i) {
int table[ncharacters] = {0};
table['/0'] = -1;
table[' '] = -2;
int s = 1;
int j;
for (j = 0; j < keysize; ++j) {
if (table[lines[i][j]] == 0) {
table[lines[i][j]] = s++;
}
if (table[lines[i][j]] != keysig[j]) {
break;
}
}
if (j == keysize) { /* we've found it */
for (j = 0; j < keysize; ++j) {
trans[lines[i][j]] = key[j];
}
found = true;
break;
}
}
if (found) {
for (int i = 0; i < nlines; ++i) {
for (int j = 0; lines[i][j] != '/0'; ++j) {
texts[i][j] = trans[lines[i][j]];
}
}
}
return found;
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
int table[ncharacters] = {0};
table['/0'] = -1;
table[' '] = -2;
int s = 1;
for (int i = 0; i < keysize; ++i) {
if (table[key[i]] == 0) {
table[key[i]] = s++;
}
keysig[i] = table[key[i]];
}
int ncases;
cin >> ncases;
cin.ignore(2048, '/n').ignore(2048, '/n');
while (ncases-- > 0) {
char lines[linecount][linesize];
int nlines = 0;
while (cin.getline(lines[nlines], linesize) && lines[nlines][0] != '/0') {
++nlines;
}
if (nlines == 0) {
cin.ignore(2048, '/n');
cout << "No solution./n";
} else {
char texts[linecount][linesize] = {{0}};
if (decrypt(lines, texts, nlines)) {
for (int i = 0; i < nlines; ++i) {
cout << texts[i] << '/n';
}
} else {
cout << "No solution./n";
}
}
if (ncases > 0) cout << '/n';
}
return 0;
}
Code:
/*************************************************************************
* Copyright (C) 2008 by liukaipeng *
* liukaipeng at gmail dot com *
*************************************************************************/
/* @JUDGE_ID 00000 850 C++ "Crypt Kicker II" */
#include <cstring>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int const linecount = 1500;
int const linesize = 100;
int const ncharacters = 128;
char key[] = "the quick brown fox jumps over the lazy dog";
int const keysize = sizeof(key);
int keysig[keysize];
bool decrypt(char lines[][linesize], char texts[][linesize], int nlines)
{
/* find the key line first */
char trans[ncharacters] = {0};
bool found = false;
for (int i = 0; i < nlines; ++i) {
int table[ncharacters] = {0};
table['/0'] = -1;
table[' '] = -2;
int s = 1;
int j;
for (j = 0; j < keysize; ++j) {
if (table[lines[i][j]] == 0) {
table[lines[i][j]] = s++;
}
if (table[lines[i][j]] != keysig[j]) {
break;
}
}
if (j == keysize) { /* we've found it */
for (j = 0; j < keysize; ++j) {
trans[lines[i][j]] = key[j];
}
found = true;
break;
}
}
if (found) {
for (int i = 0; i < nlines; ++i) {
for (int j = 0; lines[i][j] != '/0'; ++j) {
texts[i][j] = trans[lines[i][j]];
}
}
}
return found;
}
int main(int argc, char *argv[])
{
#ifndef ONLINE_JUDGE
filebuf in, out;
cin.rdbuf(in.open((string(argv[0]) + ".in").c_str(), ios_base::in));
cout.rdbuf(out.open((string(argv[0]) + ".out").c_str(), ios_base::out));
#endif
int table[ncharacters] = {0};
table['/0'] = -1;
table[' '] = -2;
int s = 1;
for (int i = 0; i < keysize; ++i) {
if (table[key[i]] == 0) {
table[key[i]] = s++;
}
keysig[i] = table[key[i]];
}
int ncases;
cin >> ncases;
cin.ignore(2048, '/n').ignore(2048, '/n');
while (ncases-- > 0) {
char lines[linecount][linesize];
int nlines = 0;
while (cin.getline(lines[nlines], linesize) && lines[nlines][0] != '/0') {
++nlines;
}
if (nlines == 0) {
cin.ignore(2048, '/n');
cout << "No solution./n";
} else {
char texts[linecount][linesize] = {{0}};
if (decrypt(lines, texts, nlines)) {
for (int i = 0; i < nlines; ++i) {
cout << texts[i] << '/n';
}
} else {
cout << "No solution./n";
}
}
if (ncases > 0) cout << '/n';
}
return 0;
}
相关文章推荐
- uva_850 - Crypt Kicker II
- uva-850-Crypt Kicker II
- uva-850-Crypt Kicker II
- UVa Problem Solution: 843 - Crypt Kicker
- UVa 850 Crypt Kicker II
- UVa 850 - Crypt Kicker II
- UVa Problem 850 Crypt Kicker II (解密 II)
- UVa Problem 843 Crypt Kicker (解密)
- Uva OJ 850 - Crypt Kicker II
- UVa Problem Solution: 706 - LC-Display
- UVa Problem Solution: 10050 - Hartals
- UVa Problem Solution: 10082 - WERTYU
- UVa Problem Solution: 10252 - Common Permutation
- UVa Problem Solution: 10194 - Football (aka Soccer)
- UVa Problem Solution: 10183 - How many fibs?
- UVa Problem Solution: 10157 - Expressions
- UVa Problem Solution: 10049 - Self-describing Sequence
- UVa Problem Solution: 10006 - Carmichael Numbers
- UVa Problem Solution: 10139 - Factovisors
- UVa Problem Solution: 10090 - Marbles