#include <cstring>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <algorithm>
using namespace std;
char line[20][6];
struct node{
int a;
int t;
};
node v[20];
int w; //单词数
int c; //参数c
int getW(int i)
{
int v = 0;
char *p = line[i];
while(*p) {
v = (v<<5) + *p-'a' + 1;
p++;
}
return v;
}
bool cmp(node a, node b)
{
return a.t < b.t;
}
bool cmp2(node a, node b)
{
return a.a < b.a;
}
int next(int val)
{
return (c/val + 1)*val;
}
void solve()
{
c=0;
for(int i=0; i<w; i++) {
v[i].a = getW(i);
}
sort(v, v+w, cmp2);
c = v[w-2].a; //
int ok = 0;
while(!ok) {
ok = 1;
for(int i=0; i<w; i++) {
v[i].t = (c/v[i].a)%w;
}
sort(v, v+w, cmp);
int i;
int max_c = c;
for(i=0; i<w-1; i++) {
if(v[i].t == v[i+1].t) {
int temp = min(next(v[i].a), next(v[i+1].a));
if(temp > max_c) max_c = temp;
ok = 0;
}
}
c = max_c;
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
char ch;
int found = 0;
w = 0;
int i = 0;
while(scanf("%c", &ch) == 1) {
putchar(ch);
if(isalpha(ch)) {
found = 1;
line[w][i++] = ch;
} else if(found) {
line[w++][i] = '\0';
i = found = 0;
if(ch == '\n') {
solve();
printf("%d\n\n", c);
w = 0;
}
}
}
return 0;
}
UVa 188 - Perfect Hash
最新推荐文章于 2020-03-08 19:50:52 发布
本文介绍了一种基于字符串特性的排序算法实现。通过对输入的字符串进行特定的数值化处理,并结合自定义的排序规则来达到字符串的有效排序。该算法适用于对一组字符串进行排序的应用场景。
9889

被折叠的 条评论
为什么被折叠?



