24h購物| | PChome| 登入
2012-05-02 08:13:45| 人氣620| 回應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
人氣(620) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][DP] 825 - Walking on the Safe Side
此分類上一篇:[UVA][Map] 902 - Password Search

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