您的位置:首页 > Web前端

UVA 188 - Perfect Hash

2013-02-17 22:17 447 查看
链结:戳我

AC了。其实就是如果C是不符合要求的,按照公式求一次,然后再代回去检验看一下行不行。

如果可以就输出;

不行的话就Refresh一下C的值,然后再重头开始检验。

View Code

/*
Author: frankdj
State: AC
Method: Simple conduct step by step what the problem said
*/

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;

const int MAXIN = 100;
const int MAXLENGTH = 14;
const int MAXWORD = 6;

void convert_integer(int words[], char word[], int& n_word) {
int length = strlen(word), rep = 0;
for (int i = 0; i < length; i++){
int index = word[i] - 'a' + 1;
rep = (rep<<5) + index;
}
words[n_word] = rep;
n_word++;
}

int compare(const void* a, const void* b) {
return (*((int*)a)) - (*((int*) b));
}

int find(int words[], int n_word) {
int c = words[0];
bool is_changed = true, is_found = false;
while (is_changed){
is_changed = false;
for (int i = 0; !is_changed && i < n_word; i++){
int hash_i = (c/words[i]) % n_word;

for (int j = i+1; !is_changed && j < n_word; j++){
int hash_j = (c/words[j]) % n_word;
if (hash_i == hash_j){
is_changed = true;
c = min((c/words[i]+1)*words[i],
(c/words[j]+1)*words[j]);
}
}
}
}
return c;
}

int main(int argc, char *argv[]){

#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
int n_word, C;
int words[MAXLENGTH];
char line[MAXIN], word[MAXWORD];

memset(line, 0, sizeof(line));
while (cin.getline(line, MAXIN) && line[0] != EOF){
memset(words, 0, sizeof(words));
C = n_word = 0;

int length = strlen(line);
for (int i = 0; i < length; i++){
memset(word, 0, sizeof(word));
while (line[i] == ' ' || line[i] == '\0')
i++;
int counter = 0;
while (isalpha(line[i]))
word[counter++] = line[i++];

convert_integer(words, word, n_word);
}
qsort(words, n_word, sizeof(int), compare);
C = find(words, n_word);
cout << line << endl;
cout << C << endl << endl;

}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: