uva 188 - Perfect Hash 模拟

简介:

 

     这题只要读懂题就等于没难度了,完全照着题目给的公式模拟即可

     要注意的就是末尾的多余空格。

 

/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#define INF 1E9
using namespace std;
char org[80],t[20];
int calc()
{
    int now=0;
    for(int i=0;t[i];i++)
        now=(now<<5)+t[i]-'a'+1;
    return now;
}
int w[20];
int main()
{
    int C,pos,len,n,now,i,j,a,b;
    while(gets(org)!=NULL)
    {
        if(org[0]=='\0')break;
        C=INF;pos=-1;n=0;
        len=strlen(org);
        while(++pos<len)
        {
            if(!isalpha(org[pos]))continue; //跳过空格
            if(sscanf(org+pos,"%s",t)==0)break;//这句话木有用……
            w[n++]=calc();
            C=min(C,w[n-1]);
            pos+=strlen(t);
        }
        bool flag=1;
        while(flag)
        {
            flag=0;
            for(i=0;i<n;i++)
             for(j=i+1;j<n;j++)
             {
                 a=C/w[i];b=C/w[j];
                 if(a%n==b%n)
                 {
                     C=min((a+1)*w[i],(b+1)*w[j]);
                     flag=1;
                 }
             }
        }
        printf("%s\n%d\n\n",org,C);
    }
}


 

 

 

目录
相关文章
|
5月前
|
Java
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
hdu1016 Prime Ring Problem【素数环问题(经典dfs)】
24 0
|
6月前
codeforces 317 A Perfect Pair
我先排除了输出-1的,然后再考虑如何计算最小的步数。我们主要在每一步中最小一个加上另一个就可以了,这是朴素的求法,但可能出现这样的情况 比如 -100000000 1 10000000 这样的话会循环100000000多次,肯定超时,所以我们要加快速度。
21 0
|
7月前
|
Go
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
Shortest Path with Obstacle( CodeForces - 1547A )(模拟)
28 0
|
8月前
UVa11157 - Dynamic Frog(动态规划)
UVa11157 - Dynamic Frog(动态规划)
36 0
|
9月前
|
文件存储
Easy Number Challenge(埃式筛思想+优雅暴力)
Easy Number Challenge(埃式筛思想+优雅暴力)
59 0
|
人工智能 BI
[UVA 1599] Ideal Path | 细节最短路
Description New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci .
176 0