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;
- }