[UVA][Trie] 902 - Password Search@Morris' Blog|PChome Online 人新台
2012-05-02 08:13:45| 人645| 回0 | 上一篇 | 下一篇

[UVA][Trie] 902 - Password Search

0 收藏 0 0 站台

Problem C

Password Search

Being able to send encoded messages during World War II was very important to the Allies. The messages were always sent after being encoded with a known password. Having a fixed password was of course insecure, thus there was a need to change it frequently. However, a mechanism was necessary to send the new password. One of the mathematicians working in the cryptographic team had a clever idea that was to send the password hidden within the message itself. The interesting point was that the receiver of the message only had to know the size of the password and then search for the password within the received text.

A password with size N can be found by searching the text for the most frequent substring with N characters. After finding the password, all the substrings that coincide with the password are removed from the encoded text. Now, the password can be used to decode the message.

Problem

Your mission has been simplified as you are only requested to write a program that, given the size of the password and the encoded message, determines the password following the strategy given above.

To illustrate your task, consider the following example in which the password size is three (N=3) and the text message is just baababacb. The password would then be aba because this is the substring with size 3 that appears most often in the whole text (it appears twice) while the other six different substrings appear only once (baa ; aab ; bab ; bac ; acb).

Input

The input file contains several test cases, each of them consists of one line with the size of the password, 0 < N ≤ 10, followed by the text representing the encoded message. To simplify things, you can assume that the text only includes lower case letters.

Output

For each test case, your program should print as output a line with the password string.

Sample Input

3 baababacb 

Sample Output

aba 



//C++ 0.384
#include <stdio.h>
#include <string.h>

char str[1000000];
struct Trie {
int n;
int link[26];
} Node[2000000];
int TrieSize;
void insertTrie(const char* str) {
static int i, idx;
idx = 0;
for(i = 0; str[i]; i++) {
if(Node[idx].link[str[i]-'a'] == 0) {
TrieSize++;
memset(&Node[TrieSize], 0, sizeof(Node[0]));
Node[idx].link[str[i]-'a'] = TrieSize;
}
idx = Node[idx].link[str[i]-'a'];
}
Node[idx].n ++;
}
int ans = 0, n;
char ansArr[20];
void travelTrie(char *str, int idx) {
if(Node[idx].n > ans) {
*str = '\0';
memcpy(ansArr, str-n, n);
ans = Node[idx].n;
}
for(int i = 0; i < 26; i++) {
if(Node[idx].link[i] != 0) {
*str = i+'a';
travelTrie(str+1, Node[idx].link[i]);
}
}
}
int main() {
int i;
while(scanf("%d %s", &n, str) == 2) {
int len = strlen(str);
char ch;
TrieSize = 0;
memset(&Node[0], 0, sizeof(Node[0]));
ans = 0;
for(i = n; i <= len; i++) {
ch = str[i];
str[i] = '\0';
insertTrie(str+i-n);
str[i] = ch;
}
memset(ansArr, 0, sizeof(ansArr));
travelTrie(str, 0);
printf("%s\n", ansArr);
}
return 0;
}

台: Morris
人(645) | 回(0)| 推 (0)| 收藏 (0)|
全站分: 不分 | 人分: UVA |
此分下一篇:[UVA][DP] 825 - Walking on the Safe Side
此分上一篇:[UVA][Map] 902 - Password Search

是 (若未登入"人新台"看不到回覆唷!)
* 入:
入片中算式的果(可能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