[UVA][KMP] 12604 - Caesar Cipher@Morris' Blog|PChome Online 人新台
2013-12-05 15:57:13| 人2,916| 回0 | 上一篇 | 下一篇

[UVA][KMP] 12604 - Caesar Cipher

0 收藏 0 0 站台

Problem C: Caesar cipher

In cryptography, a Caesar cipher, also known as Caesar's cipher, the shift cipher, Caesar's code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet (wrapping around in the end). For example, given an alphabet of capital letters in usual order, with a shift of 3, A would be replaced by D, B would become E, and so on, with Z being replaced by C. The method is named after Julius C Caesar, who used it in his private correspondence.

We are given an alphabet A, a string S which is encrypted using a shift cipher and a plaintext word W.
Find the possible values of shifts (in the range [0, |A|-1]) used in encryption if we know that the unencrypted text contains exactly one occurrence of the word W.

Input Format

Input starts with an integer N on a line, the number of test cases. Each cases contains three strings on separate lines, alphabet A, plaintext word W and encrypted text S. Alphabet A will contain only letters and digits ([A-Z][a-z][0-9]) and its symbol order is not necessarily lexicographical (see the third sample case). A will not contain duplicate symbols. The constraints are as given: 3<=|A|<=62, 1<=|W|<=50,000, 3<=|S|<=500,000.

Output Format

For each test case print one line of output. If there are no shifts that would satisfy the condition of W being a part of the unencrypted S, print "no solution". If there is exactly one shift that could have been used, print "unique: #" where # is the shift value. It there are more than one possible shifts print "ambiguous: " followed by the sorted list of shift values.
For clarification, see the sample output.

Sample Input

4 ABC ABC ABCBBBABC ABC ABC ABCBCAABC D7a D7a D7aaD77aDD7a ABC ABC ABC 

Sample Output

no solution unique: 1 ambiguous: 1 2 unique: 0

加密法,A 作基的一般字母序表。
位移也必查找 A 表。
而在明文 W,加密文件 S。
找到可能的位移,使得 S 只出在 加密後的 W 一次。

做 KMP。

#include <stdio.h>
#include <string.h>
char A[1024], W[500005], S[500005];
int kmpTable[500005];
void KMPtable(char *P, int len) {
int i, j;
kmpTable[0] = -1, i = 1, j = -1;
while(i < len) {
while(j >= 0 && P[j+1] != P[i])
j = kmpTable[j];
if(P[j+1] == P[i])
j++;
kmpTable[i++] = j;
}
}
int KMPMatching(char T[], char P[], int tlen, int plen) {
int i, j;
int matchCnt = 0;
for(i = 0, j = -1; i < tlen; i++) {
while(j >= 0 && P[j+1] != T[i])
j = kmpTable[j];
if(P[j+1] == T[i]) j++;
if(j == plen-1) {
matchCnt++;
j = kmpTable[j];
if(matchCnt >= 2)
return matchCnt;
}
}
return matchCnt;
}
char E[500005];
int main() {
int testcase;
int i, j, k;
scanf("%d", &testcase);
while(getchar() != '\n');
while(testcase--) {
gets(A);
gets(W);
gets(S);
int Alen = strlen(A);
int Wlen = strlen(W);
int Slen =strlen(S);
int CHAR[1024] = {};
for(i = 0; A[i]; i++)
CHAR[A[i]] = i;
int ret[1024], ridx = 0;
for(i = 0; i < Alen; i++) {
for(j = 0; j < Wlen; j++)
E[j] = A[(CHAR[W[j]]+i)%Alen];
E[Wlen] = '\0';
KMPtable(E, Wlen);
int cnt = KMPMatching(S, E, Slen, Wlen);
if(cnt == 1)
ret[ridx++] = i;
}
if(ridx == 0)
puts("no solution");
else if(ridx == 1)
printf("unique: %d\n", ret[0]);
else {
printf("ambiguous:");
for(i = 0; i < ridx; i++)
printf(" %d", ret[i]);
puts("");
}
}
return 0;
}

台: Morris
人(2,916) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 教育(修、留、研究、教育概) | 人分: UVA |
此分下一篇:[UVA] 11601 - Avoiding Overlaps
此分上一篇:[UVA] 12643 - Tennis Rounds

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能0) 
(有*必填)
TOP
全文
ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86