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
  • 下载

Understanding disk failure rates What

  • 2011年10月04日 03:36
  • 618KB
  • 下载

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

discount factor, discount rate, bond valuation, forward rates, spot rates
  • peachpi
  • peachpi
  • 2011年05月13日 11:19
  • 3043

Exchange 2010之安装前准备AD站点

1. 准备架构 从开始菜单--管理工具――windows powershell modules,导航到安装介质目录下执行以下命令以进行准备架构的操作 /p准备的意思prepare,s是架构的意思S...

Exchange 2016边缘传输服务器“MSExchange运行状况管理器”无法启动

在部署Exchange Server 2016会出现提示“Microsoft Exchange运行状况管理器”服务无法启动,并在日志中提示如下错误:解决办法如下:打开Exchange edge主机的系...

Exchange Server 2013 一步步安装图解

前言: 这是一份为Exchange菜鸟准备的2013版本安装文档,安装环境为Windows Server 2012 DataCenter 版本,在安装之前,我对安装Exchange也是处于一无所知...

rabbit常用三种exchange类型和python接入脚本

现在很多开源软件都提供了对应的web管理界面,rabbitmq也不例外,rabbitmq提供了一个web插件。当rabbit-server启动之后,即在浏览器中通过http://localhost:1...
  • hellotb
  • hellotb
  • 2015年10月25日 14:45
  • 261
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Exchange Rates
举报原因:
原因补充:

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