Exchange Rates

翻译 2015年11月19日 13:55:57

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;
}


相关文章推荐

Flash动画学习指南二:帧频(Frame rates)

转自:http://bbs.9ria.com/thread-72759-1-1.html 帧频,即动画播放的速度,或时间轴上播放头滑动的距离,它是通过每秒中播放的帧数来衡量的。如果帧频太低,...

802.11ac WiFi Rates

  • 2016-05-29 01:18
  • 207KB
  • 下载

证券基础知识——与bond相关的几个知识点discount factor,Spot rates,forward rates

discount factor, discount rate, bond valuation, forward rates, spot rates

[排序]Foreign Exchange uva10763

一到简单的排序题。 题意: 有一个非营利性的国际性机构执行一项国际交换学生计画。这几年来执行得相当好,有越来越多的学生想要参与这项计画,所以现在请你来帮忙这个机构。每个提出交换学生申请的人有2项资料:...

poj 1860 Currency Exchange

链接:http://poj.org/problem?id=1860 题目大意是有N个国家让你通过一定本钱在各个国家之间通过汇率转换来达到收益的目的。 最短路问题,因为出现了环,所以用SPFA来判断正环...

音频界的瑞士军刀 --- SoX - Sound eXchange 国外开源音频处理软件介绍(一)

今天我给大家介绍一款自称是音频处理界的瑞士军刀的一款开源软件,SoX - Sound eXchange。引用主页上介绍的原话:         Welcome to the home of So...

使用exchange方式切换普通表到分区表

随着数据库数据量的不断增长,有些表需要由普通的堆表转换为分区表的模式。有几种不同的方法来对此进行操作,诸如导出表数据,然后创建分区表再导入数据到分区表;使用EXCHANGE PARTITION方式来转...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)