Aizu 2242 Era Name

Time Limit:8000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu

Description

As many of you know, we have two major calendar systems used in Japan today. One of them is Gregorian calendar which is widely used across the world. It is also known as “Western calendar” in Japan.

The other calendar system is era-based calendar, or so-called “Japanese calendar.” This system comes from ancient Chinese systems. Recently in Japan it has been a common way to associate dates with the Emperors. In the era-based system, we represent a year with an era name given at the time a new Emperor assumes the throne. If the era name is “A”, the first regnal year will be “A 1”, the second year will be “A 2”, and so forth.

Since we have two different calendar systems, it is often needed to convert the date in one calendar system to the other. In this problem, you are asked to write a program that converts western year to era-based year, given a database that contains association between several western years and era-based years.

For the simplicity, you can assume the following:

  1. A new era always begins on January 1st of the corresponding Gregorian year.
  2. The first year of an era is described as 1.
  3. There is no year in which more than one era switch takes place.

Please note that, however, the database you will see may be incomplete. In other words, some era that existed in the history may be missing from your data. So you will also have to detect the cases where you cannot determine exactly which era the given year belongs to.

Input

The input contains multiple test cases. Each test case has the following format:

N Q
EraName
1EraBasedYear1WesternYear1
.
.
.
EraNameNEraBasedYearNWesternYearN
Query1 .
.
.
QueryQ

The first line of the input contains two positive integers N and Q (1 ≤ N ≤ 1000, 1 ≤ Q ≤ 1000). N is the number of database entries, and Q is the number of queries.

Each of the following N lines has three components: era name, era-based year number and the corresponding western year (1 ≤EraBasedYeari ≤ WesternYeari ≤ 109 ). Each of era names consist of at most 16 Roman alphabet characters. Then the last Q lines of the input specifies queries (1 ≤ Queryi ≤ 109 ), each of which is a western year to compute era-based representation.

The end of input is indicated by a line containing two zeros. This line is not part of any dataset and hence should not be processed.

You can assume that all the western year in the input is positive integers, and that there is no two entries that share the same era name.

Output

For each query, output in a line the era name and the era-based year number corresponding to the western year given, separated with a single whitespace. In case you cannot determine the era, output “Unknown” without quotes.

Sample Input

4 3
meiji 10 1877
taisho 6 1917
showa 62 1987
heisei 22 2010
1868
1917
1988
1 1
universalcentury 123 2168
2010
0 0

Output for the Sample Input

meiji 1
taisho 6
Unknown
Unknown
#include <iostream>
#include <string>
#include <string.h>

using namespace std;

#define MAXN 1001

struct Node
{
    string name;
    int a, b;
}node[MAXN];


int n, m;

void input()
{
    int a, b;

    while (cin >> n >> m)
    {
        if (!n && !m)
        {
            break;
        }

        for (int i = 0; i < n; i++)
        {
            cin >> node[i].name;
            cin >> a >> b;
            node[i].a = b - a + 1;
            node[i].b = b;
        }

        for (int i = 0; i < m; i++)
        {
            cin >> a;
            bool flag = false;

            for (int i = 0; i < n; i++)
            {
                if (node[i].a <= a && a <= node[i].b)
                {
                    cout << node[i].name << ' ' << a - node[i].a + 1<< endl;
                    flag = true;
                    break;
                }
            }

            if (!flag)
            {
                cout << "Unknown" << endl;
            }
        }
    }
}

int main()
{
    std::ios::sync_with_stdio(false);
    input();
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值