真正的危机不是机器人像人一样思考,而是人像机器一样思考。 ——凉宫春日的忧郁

[国家集训队2011]悄悄话

[国家集训队2011]悄悄话

题目

在这个有话不直说的年代,密码学越来越被广泛接受。

我们引用经典的“凯撒密码”。

在英文中,凯撒加密只对26个字母生效(分大小写)

我们按照’a’到’z’来排字母。

凯撒加密的原理就是把原文的每一个字母都按顺序往后移K位。这个K将被作为密钥。(’a’往后移变成’b’,’z’往后移会变成’a’)

(0 ≤ K ≤ 25)

现在给出一系列用凯撒加密的英文句子,请你编写程序逐句翻译。

也就是说,请你确定一个密钥,使得解码以后的文字最符合英文的规则与规范。

数据保证存在唯一的解码方案,使得明码是完全可以分辨的英文句子。

INPUT

输入一定包括10行

每一行都是用同一密钥加密的英文

OUTPUT

输出10行,为解密结果。不允许格式上有任何不同。

SAMPLE

INPUT

Welcome to the test. This is the 1st sample test case.

Vdkbnld sn sgd sdrs. Sghr hr sgd 2mc rzlokd sdrs bzrd.

Welcome to the test. This is the 3rd sample test case.

Nvctfdv kf kyv kvjk. Kyzj zj kyv 4ky jrdgcv kvjk trjv.

Govmywo dy dro docd. Drsc sc dro 5dr ckwzvo docd mkco.

Nvctfdv kf kyv kvjk. Kyzj zj kyv 6ky jrdgcv kvjk trjv.

Jrypbzr gb gur grfg. Guvf vf gur 7gu fnzcyr grfg pnfr.

Ucjamkc rm rfc rcqr. Rfgq gq rfc 8rf qyknjc rcqr ayqc.

Ckriusk zu znk zkyz. Znoy oy znk 9zn ygsvrk zkyz igyk.

Xfmdpnf up uif uftu. Uijt jt uif mbtu tbnqmf uftu dbtf.

OUTPUT

Welcome to the test. This is the 1st sample test case.

Welcome to the test. This is the 2nd sample test case.

Welcome to the test. This is the 3rd sample test case.

Welcome to the test. This is the 4th sample test case.

Welcome to the test. This is the 5th sample test case.

Welcome to the test. This is the 6th sample test case.

Welcome to the test. This is the 7th sample test case.

Welcome to the test. This is the 8th sample test case.

Welcome to the test. This is the 9th sample test case.

Welcome to the test. This is the last sample test case.

解题报告

毒瘤= =

我会说我对着数据扩展我的词典吗qwq

首先,我们肯定要得到这26个字符串,然后,对每个字符串进行估值,判断每个串是正常句子的可能性,找到最大的作为答案

所以问题就在于如何搞这个可能性,我们需要一个估值函数,利用字母出现的频率与是否出现常见单词(这tm就是那个词典啊QWQ)来估值,得到每个串的估计值并选择最大的字符串

真是毒瘤啊qwq

 1 #include <iostream>
 2 #include <cstring>
 3 #include <string>
 4 #include <cstdio>
 5 #include <set>
 6 using namespace std;
 7 string dic[34]={"i","you","me","he","she","this","that","to","in","on","is","are","test","the","and","but","we","now","case","do","only","a","girl","there","here","my","it","of","all","was","how","what","they","good"};
 8 set<string>s;
 9 int typ[10005],val[26],len;
10 string txt[26];
11 char common[26]={'a','e','i','t','n','r','s','o'},rear[26]={'b','j','p','q','v','w','x','z'};
12 inline void pre(){
13     for(int i=0;i<34;++i)s.insert(dic[i]);
14     for(int i=0;i<7;++i)++val[common[i]-'a'],--val[rear[i]-'a'];
15 }
16 inline bool low(char x){return 'a'<=x&&x<='z';}
17 inline void turn(string &x){
18     for(int i=0;i<len;++i){
19         if('A'<=x[i]&&x[i]<='Z'){
20             x[i]+='a'-'A';
21             typ[i]=1;
22         }
23         else if(!low(x[i]))typ[i]=2;
24     }
25 }
26 inline void trans(string x,string &y,int w){
27     y="\0";
28     for(int i=0;i<len;++i){
29         if(low(x[i]))y+='a'+(x[i]-'a'+w)%26;
30         else y+=x[i];
31     }
32 //    cout<<w<<endl<<y<<endl;
33 }
34 inline int cal(string x){
35     int ret(0);
36     for(int i=0;i<len;++i){
37         ret+=val[x[i]-'a'];
38         if(x[i]=='i'&&typ[i]==1){
39             ret+=5;
40             if(i+1<len&&x[i+1]=='\'')ret+=3;
41         }
42         if(x[i]=='\''&&i+1<len&&(x[i+1]=='s'||x[i+1]=='m'))ret+=4;
43     }
44     int i(0),j;
45     while(i<len){
46         while(!low(x[i])&&i<len)++i;
47         j=i;string tmp("\0");
48         while(low(x[j])&&j<len)tmp+=x[j++];
49 //        cout<<tmp<<endl;
50         if(s.count(tmp))ret+=10;
51         i=j;
52     }
53 //    cout<<x<<' '<<ret<<endl;
54     return ret;
55 }
56 inline void print(string x){
57     for(int i=0;i<len;++i){
58         if(typ[i]!=1)putchar(x[i]);
59         else putchar(x[i]-'a'+'A');
60     }
61     putchar('\n');
62 }
63 int main(){
64     freopen("msg.in","r",stdin);freopen("msg.out","w",stdout);
65     pre();
66     for(int i=1;i<=10;++i){
67         memset(typ,0,sizeof(typ));getline(cin,txt[0]);len=txt[0].size();turn(txt[0]);
68         for(int j=1;j<26;++j)trans(txt[0],txt[j],j);
69         int mx(0),ans(0);
70         for(int j=0;j<26;++j){int tmp(cal(txt[j]));if(tmp>mx)mx=tmp,ans=j;}
71         print(txt[ans]);
72     }
73 }
View Code

 

posted @ 2017-10-26 15:07  Hzoi_Mafia  阅读(237)  评论(0编辑  收藏  举报
我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。 ——死神