Codeforces Round #459 (Div. 2):B. Radio Station

B. Radio Station

time limit per test2 seconds
memory limit per test256 megabytes

Problem Dsecription

As the guys fried the radio station facilities, the school principal gave them tasks as a punishment. Dustin’s task was to add comments to nginx configuration for school’s website. The school has n servers. Each server has a name and an ip (names aren’t necessarily unique, but ips are). Dustin knows the ip and name of each server. For simplicity, we’ll assume that an nginx command is of form “command ip;” where command is a string consisting of English lowercase letter only, and ip is the ip of one of school servers.
这里写图片描述

Each ip is of form “a.b.c.d” where a, b, c and d are non-negative integers less than or equal to 255 (with no leading zeros). The nginx configuration file Dustin has to add comments to has m commands. Nobody ever memorizes the ips of servers, so to understand the configuration better, Dustin has to comment the name of server that the ip belongs to at the end of each line (after each command). More formally, if a line is “command ip;” Dustin has to replace it with “command ip; #name” where name is the name of the server with ip equal to ip.

Dustin doesn’t know anything about nginx, so he panicked again and his friends asked you to do his task for him.

Input

The first line of input contains two integers n and m (1 ≤ n, m ≤ 1000).

The next n lines contain the names and ips of the servers. Each line contains a string name, name of the server and a string ip, ip of the server, separated by space (1 ≤ |name| ≤ 10, name only consists of English lowercase letters). It is guaranteed that all ip are distinct.

The next m lines contain the commands in the configuration file. Each line is of form “command ip;” (1 ≤ |command| ≤ 10, command only consists of English lowercase letters). It is guaranteed that ip belongs to one of the n school servers.

Output

Print m lines, the commands in the configuration file after Dustin did his task.

Examples

input

2 2
main 192.168.0.2
replica 192.168.0.1
block 192.168.0.1;
proxy 192.168.0.2;

output

block 192.168.0.1; #replica
proxy 192.168.0.2; #main

input

3 5
google 8.8.8.8
codeforces 212.193.33.27
server 138.197.64.57
redirect 138.197.64.57;
block 8.8.8.8;
cf 212.193.33.27;
unblock 8.8.8.8;
check 138.197.64.57;

output

redirect 138.197.64.57; #server
block 8.8.8.8; #google
cf 212.193.33.27; #codeforces
unblock 8.8.8.8; #google
check 138.197.64.57; #server


解题心得:

  1. 题目说了一大堆其实题意很简单,给你n个ip地址,每个地址有一个对应的名字,然后m次询问,每次询问给你一个ip地址,叫你输出ip地址的名字。当然还有一些乱七八糟的输出要求。
  2. 就是一个hash,将每个地址用一个数表示,然后将地址和这个数存起来,每次询问的时候直接去找这个数,然后将地址输出就行了。主要的是考验一个读题的能力。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5+100;
map <string ,string> maps;
string s1;
int n,m,tot=0;
struct NODE
{
    string s;
    long long num;
}node[10000];
int main()
{
   cin>>n>>m;
   for(int i=0;i<n;i++)
   {
       cin>>s1;
       long long x1,x2,x3,x4,ans =0;
       scanf("%lld.%lld.%lld.%lld",&x1,&x2,&x3,&x4);//注意输入的时候控制格式
       ans += x1*233;
       ans = ans*233+x2;
       ans = ans*233+x3;
       ans = ans*233+x4;
       node[tot].num = ans;
       node[tot++].s = s1;
   }
   for(int i=0;i<m;i++)
   {
       cin>>s1;
       long long x1,x2,x3,x4,ans=0;
       scanf("%lld.%lld.%lld.%lld;",&x1,&x2,&x3,&x4);
       cout<<s1<<" ";
       printf("%lld.%lld.%lld.%lld; #",x1,x2,x3,x4);
       ans = x1*233;
       ans = ans*233+x2;
       ans = ans*233+x3;
       ans = ans*233+x4;
       for(int i=0;i<tot;i++)
           if(node[i].num == ans)
           {
               cout<<node[i].s<<endl;
               break;
           }
   }
   return 0;
}
原文地址:https://www.cnblogs.com/GoldenFingers/p/9107188.html