Exchange Rates

来源:互联网 发布:dms软件 编辑:程序博客网 时间:2024/06/04 19:07

Description

Using money to pay for goods and services usually makes life easier, but sometimes people prefer to trade items directly without any money changing hands. In order to ensure a consistent "price", traders set an exchange rate between items. The exchange rate between two items A and B is expressed as two positive integers m and n, and says that m of item A is worth n of item B. For example, 2 stoves might be worth 3 refrigerators. (Mathematically, 1 stove is worth 1.5 refrigerators, but since it's hard to find half a refrigerator, exchange rates are always expressed using integers.)

Your job is to write a program that, given a list of exchange rates, calculates the exchange rate between any two items.


Input

The input file contains one or more commands, followed by a line beginning with a period that signals the end of the file. Each command is on a line by itself and is either an assertion or a query. An assertion begins with an exclamation point and has the format

! m itema = n itemb

where itema and itemb are distinct item names and m and n are both positive integers less than 100. This command says that m of itema are worth n of itemb. A query begins with a question mark, is of the form

? itema = itemb

and asks for the exchange rate between itema and itemb, where itema and itemb are distinct items that have both appeared in previous assertions (although not necessarily the same assertion).


Output

For each query, output the exchange rate between itema and itemb based on all the assertions made up to that point. Exchange rates must be in integers and must be reduced to lowest terms. If no exchange rate can be determined at that point, use question marks instead of integers. Format all output exactly as shown in the example.

Note:

>Item names will have length at most 20 and will contain only lowercase letters.
>Only the singular form of an item name will be used (no plurals).
>There will be at most 60 distinct items.
>There will be at most one assertion for any pair of distinct items.
>There will be no contradictory assertions. For example, "2 pig = 1 cow", "2 cow = 1 horse", and "2 horse = 3 pig" are contradictory.
>Assertions are not necessarily in lowest terms, but output must be.
>Although assertions use numbers less than 100, queries may result in larger numbers that will not exceed 10000 when reduced to lowest terms.


Sample Input

! 6 shirt = 15 sock
! 47 underwear = 9 pant
? sock = shirt
? shirt = pant
! 2 sock = 1 underwear
? pant = shirt
.


Sample Output

5 sock = 2 shirt
? shirt = ? pant

45 pant = 188 shirt


原文:http://accepted.com.cn/zoj1705/


代码只是翻译原文中的代码,开始用搜索不知道为什么不行,这道题采用了

类似并查集合并父节点的形式,具体计算需要自己理解。

#include <cstdio>#include <iostream>#include <string>#include <cstring>using namespace std;const int maxn = 1005;int u, v, father[maxn][3], cnt;string name[maxn], name1, name2;int find ( string str ){    for ( int i = 0; i < cnt; i ++ )    //查询str所在下标        if ( str == name[i] )            return i;    return -1;}template < class T >void Swap ( T &a, T &b )    //交换{    T t = a;    a = b;    b = t;}int GCD ( int a, int b )    //公约数{    if ( a < b )        Swap ( a, b );    while ( a%b )    {        int t = a%b;        a = b;        b = t;    }    return b;}int solve ( int pos, int & v1, int & v1a ){    int a = father[pos][1], b = father[pos][2], v2, v2a;    if ( father[pos][0] == pos )    //当父节点是本身时    {        v1 = 1;        v1a = 1;        return pos; //返回下标,直接连接父节点(和并查集有点像)    }    father[pos][0] = solve ( father[pos][0], v2, v2a );    father[pos][1] = v2*a;  //转换    father[pos][2] = v2a*b;    v1 = father[pos][1];    v1a = father[pos][2];    int gcd = GCD ( v1, v1a );  //去掉公因子    father[pos][1] = father[pos][1]/gcd, father[pos][2] = father[pos][2]/gcd;    return father[pos][0];}void deal ( ){    int x, y, pos, gcd;    x = find ( name1 );    y = find ( name2 );    gcd = GCD ( u, v );    u = u/gcd;    v = v/gcd;    //printf ( "%d %d\n", x, y );    if ( x != -1 && y != -1 )    {        int v1, v1a, v2, v2a;        int sx = solve ( x, v1, v1a );        int sy = solve ( y, v2, v2a );        //printf ( "**%d %d %d %d\n", v1, v1a, v2, v2a );        father[sx][0] = sy;        father[sx][1] = v2*v1a*v;   //1保存sx->sy需要的数量        father[sx][2] = v1*v2a*u;   //2保存sy->sx需要的数量        gcd = GCD ( father[sx][1], father[sx][2] );        father[sx][1] = father[sx][1]/gcd, father[sx][2] = father[sx][2]/gcd;    }    if ( x != -1 && y == -1 )    {        Swap ( u, v );  //将x交换成-1        Swap ( x, y );        Swap ( name1, name2 );    }    if ( y == -1 )    {        y = cnt ++; //将name2保存并设置为父节点        name[y] = name2;        father[y][0] = y;        father[y][1] = father[y][2] = 1;    }    x = cnt ++;    name[x] = name1;    father[x][0] = y;    father[x][1] = v;   //y的数量    father[x][2] = u;}void query ( ){    int x, y, sx, sy, v1, v1a, v2, v2a;    x = find ( name1 );    y = find ( name2 );    sx = solve ( x, v1, v1a );    sy = solve ( y, v2, v2a );    if ( sx != sy )        cout << "? " << name1 << " = ? " << name2 << endl;    else    {        int ans1 = v2*v1a;  //左边需要的数量        int ans2 = v1*v2a;        int gcd = GCD ( ans1, ans2 );        cout << ans1/gcd << " " << name1 << " = " << ans2/gcd        << " " << name2 << endl;    }}int main ( ){    string op, eq;    while ( cin >> op && op[0] != '.' )    {        if ( op[0] == '!' )        {            cin >> u >> name1 >> eq >> v >> name2;            deal ( );        }        else        {            cin >> name1 >> eq >> name2;            query ( );        }    }    return 0;}


0 0
原创粉丝点击