SPOJ9122 字符串模拟

Diary
 
 
 
Problem description
Nowadays, people who want to communicate in a secure way use asymmetric encryption algorithms such as RSA. However, my older brother uses another, simpler encryption method for his diary entries. He uses a substitution cipher where each letter in the plaintext is substituted by another letter from the alphabet. The distance between the plaintext letter and the encrypted letter is fixed. If we would define this fixed distance d to 5, A would be replaced by F, B by G, C by H, ..., Y by D, Z by E.

 

With a fixed and known distance d the decryption would be somewhat simple. But my brother uses random distances for each of his diary entries. To decrypt his diary I have to guess the distance d for each entry. Thus, I use the well known phenomenon that the letter E is used more often in English words than other letters. Can you write a program for me that calculates the distance d based on the fact that the most used letter in the encrypted text corresponds to the letter E in plaintext? Of course, I am interested in the decrypted text, too.

Input
The input consists of several test cases c that follow (1 ≤ c ≤ 100). Each test case is given in exactly one line containing one diary entry. Diary entries only use upper case letters (A-Z) and spaces. Each diary entry consists of at most 1000 encrypted letters (including spaces).
Output
For each test case, print one line containing the smallest possible distance d (0 ≤ d ≤ 25) and the decrypted text. If the decryption is not possible because there are multiple distances conforming to the rules above, print NOT POSSIBLE instead. Spaces are not encrypted.
Sample Input
4
RD TQIJW GWTYMJWX INFWD JSYWNJX ZXJ F XNRUQJ JSHWDUYNTS YJHMSNVZJ
THE QUICK BROWN FOX JUMPS OVER THE LAZY DOG
XVIDRE TFCCVXZRKV GIFXIRDDZEX TFEKVJK UVTIPGKZFE
XVIDRE TFCCVXZRKV GIFXIRDDZEX TFEKVJK
Sample Output
5 MY OLDER BROTHERS DIARY ENTRIES USE A SIMPLE ENCRYPTION TECHNIQUE
10 JXU GKYSA RHEMD VEN ZKCFI ELUH JXU BQPO TEW
17 GERMAN COLLEGIATE PROGRAMMING CONTEST DECRYPTION
NOT POSSIBLE

题意:略
思路:字符串啊字符串……我的大伤疤……从东北赛之后,我把那道WA的代码带回来一直到现在没敢打开,只知道WA,但是WA了到最后都不知道为什么WA……关键是个水题!!伤心ing
View Code
 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 #include <stdio.h>
 5 #include <string.h>
 6 using namespace std;
 7 int main()
 8 {
 9     char st[1010];
10     int t;
11     char ans[30];
12     scanf("%d",&t);
13     getchar();
14     while(t--)
15     {
16         gets(st);
17         int num[30];
18         int len=strlen(st);
19         //以下u是看是不是纯空格的情况
20         int u=0;
21         for(int i=0;i<len;i++)
22         {
23             if(st[i]!=' ') break;
24             u++;
25         }
26         if(u==len) {printf("NOT POSSIBLE\n");continue;}
27         //当不是纯空格的时候
28         int _max=-1,flag;
29         //其中max是出现的最多的次数,flag是d
30         char ch;// ch是出现次数最多的字母
31         memset(num,0,sizeof(num));
32         for(int i=0;i<len;i++)
33         {
34             if(st[i]==' ') continue;
35             num[(int)(st[i]-'A')]++;
36             if(num[(int)(st[i]-'A')]>_max)
37             {
38                 _max=num[(int)(st[i]-'A')];
39                 ch=st[i];
40                 flag=(int)(st[i]-'E');
41             }
42         }
43         int no=0;
44         for(int i=0;i<26;i++)
45         if(num[i]==_max&&(i+'A')!=ch)
46         {
47             printf("NOT POSSIBLE\n");
48             no=1;
49             break;
50         }
51         if(no) continue;
52 
53         printf("%d ",flag>=0?flag:flag+26);
54 
55         for(int i=0;i<len;i++)
56         {
57             if(st[i]==' ')printf(" ");
58             else
59             printf("%c",(st[i]-'A'-flag+26)%26+'A');
60         }
61         printf("\n");
62     }
63 }

 

posted @ 2012-09-01 15:05  _sunshine  阅读(563)  评论(0编辑  收藏  举报