Ultraviolet A Problem Solution: 850-crypt kicker II

Source: Internet
Author: User
Tags crypt

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:

  1. /*************************************** **********************************
  2. * Copyright (c) 2008 by liukaipeng *
  3. * Liukaipeng at gmail dot com *
  4. **************************************** *********************************/
  5. /* @ Judge_id 00000 850 C ++ "crypt kicker II "*/
  6. # Include <cstring>
  7. # Include <iostream>
  8. # Include <fstream>
  9. # Include <string>
  10. # Include <vector>
  11. Using namespace STD;
  12.  
  13. Int const linecount = 1500;
  14. Int const linesize = 100;
  15. Int const ncharacters = 128;
  16. Char key [] = "The quick brown fox jumps over the lazy dog ";
  17. Int const keysize = sizeof (key );
  18. Int keysig [keysize];
  19. Bool decrypt (char lines [] [linesize], char texts [] [linesize], int nlines)
  20. {
  21. /* Find the key line first */
  22. Char trans [ncharacters] = {0 };
  23. Bool found = false;
  24. For (INT I = 0; I <nlines; ++ I ){
  25. Int table [ncharacters] = {0 };
  26. Table ['/0'] =-1;
  27. Table [''] =-2;
  28. Int S = 1;
  29. Int J;
  30. For (j = 0; j <keysize; ++ J ){
  31. If (Table [lines [I] [J] = 0 ){
  32. Table [lines [I] [J] = s ++;
  33. }
  34. If (Table [lines [I] [J]! = Keysig [J]) {
  35. Break;
  36. }
  37. }
  38. If (j = keysize) {/* We 've found it */
  39. For (j = 0; j <keysize; ++ J ){
  40. Trans [lines [I] [J] = Key [J];
  41. }
  42. Found = true;
  43. Break;
  44. }
  45. }
  46. If (found ){
  47. For (INT I = 0; I <nlines; ++ I ){
  48. For (Int J = 0; lines [I] [J]! = '/0'; ++ J ){
  49. Texts [I] [J] = trans [lines [I] [J];
  50. }
  51. }
  52. }
  53. Return found;
  54. }
  55. Int main (INT argc, char * argv [])
  56. {
  57. # Ifndef online_judge
  58. Filebuf In, out;
  59. Cin. rdbuf (in. Open (string (argv [0]) + ". In"). c_str (), ios_base: In ));
  60. Cout. rdbuf (out. Open (string (argv [0]) + ". Out"). c_str (), ios_base: Out ));
  61. # Endif
  62. Int table [ncharacters] = {0 };
  63. Table ['/0'] =-1;
  64. Table [''] =-2;
  65. Int S = 1;
  66. For (INT I = 0; I <keysize; ++ I ){
  67. If (Table [Key [I] = 0 ){
  68. Table [Key [I] = s ++;
  69. }
  70. Keysig [I] = table [Key [I];
  71. }
  72. Int ncases;
  73. Cin> ncases;
  74. Cin. Ignore (2048, '/N'). Ignore (2048,'/N ');
  75. While (ncases --> 0 ){
  76. Char lines [linecount] [linesize];
  77. Int nlines = 0;
  78. While (CIN. Getline (lines [nlines], linesize) & lines [nlines] [0]! = '/0 '){
  79. ++ Nlines;
  80. }
  81. If (nlines = 0 ){
  82. Cin. Ignore (2048, '/N ');
  83. Cout <"no solution./N ";
  84. } Else {
  85. Char texts [linecount] [linesize] = {0 }};
  86. If (decrypt (lines, texts, nlines )){
  87. For (INT I = 0; I <nlines; ++ I ){
  88. Cout <texts [I] <'/N ';
  89. }
  90. } Else {
  91. Cout <"no solution./N ";
  92. }
  93. }
  94. If (ncases> 0) cout <'/N ';
  95. }
  96. Return 0;
  97. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.