- 注册时间
- 2007-12-27
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 40155
- 在线时间
- 小时
|
楼主 |
发表于 2021-5-2 19:21:17
|
显示全部楼层
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <math.h>
- const char *raw = "BZWIEZSEHPVCIEBSYHAWWYLIIGWHMILZLMHTCSXWWCWXWWJLEIWOLMBSWPXWWXSVTZPWEXVESXWWXPMHLPRXDLSMWSJPEQXZLHATOPVIQAYHMCYDLIPNPWSUUZVRDMEMRIZPJMTDOALTFDYHSWYPCBQDLIPXCSWTSYHWIGZHYEJTKLIOSMPTQZYVHZPEZTKREXWWCIHGGFRHBAYIECVMSATVOSACLZMXWADFVDLSIVHKLMHIGSMQSGJSYXFEIRSLZVIXYYSZTJFWAXDWCSJSNXYPDWCVJDPYWPFOXLTQSEXTVSMQPDWXLTEZVIQWNEYHWZJLXKOVIPELRHLZLXLTZLHWPAO";
- #define KEY_LEN 12
- #define NUMC 26
- char freq[KEY_LEN][NUMC];
- int count[KEY_LEN];
- double prob[NUMC]={0.0788,0.0156,0.0268,0.0389,0.1268,0.0256,0.0187,0.0573,0.0707,0.0010,0.0060,0.0394,0.0244,0.0706,0.0776,0.0186,0.0009,0.0594,0.0634,0.0978,0.0280,0.0102,0.0214,0.0016,0.0202,0.0006};
- int main()
- {
- int len=strlen(raw);
- int key_len = 5;
- int i,j,k;
- double entropy=0.0;
- double var=0.0;
- for(i=0;i<NUMC;i++){
- entropy-=prob[i]*log(prob[i]);
- var+=prob[i]*log(prob[i])*log(prob[i]);
- }
- for(i=0;i<len;i++){
- char c=raw[i];
- if('a'<=c&&c<='z'){
- freq[i%KEY_LEN][c-'a']++;
- }else if('A'<=c&&c<='Z'){
- freq[i%KEY_LEN][c-'A']++;
- }else{
- fprintf(stderr, "Invalid char %c encounted\n",c);
- }
- count[i%KEY_LEN]++;
- }
- for(i=0;i<KEY_LEN;i++){
- double best_entropy = 0;
- int best_j=-1;
- for(j=0;j<NUMC;j++){
- double real_entropy=0.0;
- for(k=0;k<NUMC;k++){
- real_entropy-=freq[i][(j+k)%NUMC]*log(prob[k]);
- }
- if(best_j<0||real_entropy<best_entropy){
- best_j=j;
- best_entropy=real_entropy;
- }
- }
- double exp_entropy = count[i]*entropy;
- double exp_var = count[i]*var;
- exp_var = sqrt(exp_var-exp_entropy*exp_entropy/count[i]);
- if(best_entropy>exp_entropy+4*exp_var){
- fprintf(stderr, "\nNo valid solution found\n");
- exit(-1);
- }else{
- printf("%c",'A'+best_j);
- }
- }
- printf("\n");
- return 0;
- }
复制代码 |
评分
-
参与人数 1 | 威望 +12 |
金币 +12 |
贡献 +12 |
经验 +12 |
鲜花 +12 |
收起
理由
|
wayne
| + 12 |
+ 12 |
+ 12 |
+ 12 |
+ 12 |
|
查看全部评分
|