您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: