UVA 188 - Perfect Hash
2013-02-17 22:17
447 查看
链结:戳我。
AC了。其实就是如果C是不符合要求的,按照公式求一次,然后再代回去检验看一下行不行。
如果可以就输出;
不行的话就Refresh一下C的值,然后再重头开始检验。
View Code
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; }
相关文章推荐
- UVA 188 - Perfect Hash
- UVA - 188 Perfect Hash
- UVA - 188 Perfect Hash
- uva188 - Perfect Hash(完美哈希)
- UVa 188 - Perfect Hash
- UVA - 188 Perfect Hash(hash)
- Uva--188 Perfect Hash
- UVA 188 Perfect Hash
- UVa 188 - Perfect Hash
- uva188 Perfect Hash
- UVA 188 Perfect Hash
- uva 188 Perfect Hash(进制转换 + 遍历)
- [模拟]Perfect Hash UVA188
- UVa 188 - Perfect Hash
- 188 - Perfect Hash
- UVA 10391 Compound Words(hash字符串)
- UVA 11557 - Code Theft (KMP + HASH)
- uva704 - Colour Hash(彩色转盘)
- UVA - 10029 Edit Step Ladders (二分+hash)
- UVA 12238 Anti-Rhyme Pairs(hash+二分)