24h購物| | PChome| 登入
2012-05-12 07:56:36| 人氣645| 回應0 | 上一篇 | 下一篇

[UVA][String] 468 - Key to Success

推薦 0 收藏 0 轉貼0 訂閱站台


 Key to Success 

Any one-to-one mapping, f, of any alphabet to itself can beused to encode text by replacing each occurrence of any letter, c, withf(c).One such mapping could be the mapping of a letter tothree positions beyond the letter in the alphabet.That is, tex2html_wrap_inline35 , tex2html_wrap_inline37 , tex2html_wrap_inline39 , tex2html_wrap_inline41 and so on.

With this mapping, ``The car is blue'' will be encoded as ``Wkh fdu lv eoxh''.

Input and Output

The input begins with a single positive integer on a line by itselfindicating the number of the cases following, each of them as described below.This line is followed by a blank line, and there is also a blank line betweentwo consecutive inputs.

Your correct program should decodes the contents of each input set accordingto the following guidelines:

  1. Only letters are encoded. Letters are mapped to letters.Uppercase letters are different from their lowercase counter parts.
  2. The mapping that defines the encoding is one-to-one. Thatis, two different letters never map to the same letter of thealphabet ( tex2html_wrap_inline43 and tex2html_wrap_inline45 is impossible).
  3. There are two input lines -the first one contains a text (not encoded) andthe second one contains an encoded text.This text is to be decoded by your program.
  4. Both lines are written by the same person.
  5. It is to be assumed that anyperson uses letters of the alphabet with the same RELATIVEFREQUENCY from document to document and no two letters areused with the same frequency.That is, the most frequently used letter in the first line maps to the most frequently used letter in the second one; the second most frequently used letter maps to the second most frequently used letter and so on.

 The outputs of two consecutive cases will be separated by a blank line.

Sample Intput

1

abacxbacac
qqqqqrrrrssstt

Sample Output

aaaaaccccbbbxx




根據頻率大小找到一對一的函數, 接著重新編碼即可

#include <stdio.h>
#include <stdlib.h>

struct node {
int v, c;
node() {
v = 0;
c = 0;
}
};
int cmp(const void *i, const void *j) {
node *a, *b;
a = (node *)i, b = (node *)j;
return b->v - a->v;
}
int main() {
int t, i;
char str[10], a[10000], b[10000];
scanf("%d", &t);
gets(str);
while(t--) {
gets(str);
gets(a);
gets(b);
node aI[128], bI[128];
for(i = 0; i < 128; i++)
aI[i].c = bI[i].c = i;
for(i = 0; a[i]; i++)
aI[a[i]].v++;
for(i = 0; b[i]; i++)
bI[b[i]].v++;
qsort(aI, 128, sizeof(node), cmp);
qsort(bI, 128, sizeof(node), cmp);
int mapp[128] = {};
for(i = 0; i < 128; i++) {
mapp[bI[i].c] = aI[i].c;
}
for(i = 0; b[i]; i++)
putchar(mapp[b[i]]);
puts("");
if(t)
puts("");
}
return 0;
}

台長: Morris
人氣(645) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][String] 10062 - Tell me the frequencies!
此分類上一篇:[UVA][SA] 1223 - Editor

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文