洛谷P3405 Cities and States S

题目描述

To keep his cows intellectually stimulated, Farmer John has placed a large map of the USA on the wall of his barn. Since the cows spend many hours in the barn staring at this map, they start to notice several curious patterns. For example, the cities of Flint, MI and Miami, FL share a very special relationship: the first two letters of "Flint" give the state code ("FL") for Miami, and the first two letters of "Miami" give the state code ("MI") for Flint.

Let us say that two cities are a "special pair" if they satisfy this property and come from different states. The cows are wondering how many special pairs of cities exist. Please help them solve this amusing geographical puzzle!

为了让奶牛在智力上受到刺激,农夫约翰在谷仓的墙上放了一张美国地图。由于奶牛在谷仓里花了很多时间盯着这张地图,他们开始注意到一些奇怪的关系。例如,城市Flint,在MI省,或者Miami在FL省,他们有一种特殊的关系:“Flint”市前两个字母就是“FL”省,迈阿密前两个字母是“MI”省。

让我们说,两个城市是一个“特殊的一对”,如果他们满足这个属性,来自不同的省。奶牛想知道有多少特殊的城市存在。请帮助他们解决这个有趣的地理难题!

输入格式

The first line of input contains NN (1 \leq N \leq 200,0001≤N≤200,000), the number ofcities on the map.

The next NN lines each contain two strings: the name of a city (a string of at least 2 and at most 10 uppercase letters), and its two-letter state code (a string of 2 uppercase letters). Note that the state code may be something like ZQ, which is not an actual USA state. Multiple cities with the same name can exist, but they will be in different states.

输入的第一行包含(),地图上的城市数量。

下一行包含两个字符串:一个城市的名称(字符串至少2个最多10个大写字母),和它的两个字母的州代码(一串2个大写字母)。注意状态代码可能像ZQ,这并不是一个真正的美国。同一名称的多个城市可以存在,但它们将处于不同的州。

输出格式

Please output the number of special pairs of cities.

请输出特殊城市对数。

输入输出样例

输入 #1复制

6
MIAMI FL
DALLAS TX
FLINT MI
CLEMSON SC
BOSTON MA
ORLANDO FL

输出 #1复制

1

上代码:

#include<bits/stdc++.h>
using namespace std;
int city[676][676], n, x, y, ans;//x表示城市前两个字母的26进制下的数,y是省份的。数相等则对应的省的两个字母和城市的前两个字母就相等。
int main() {
    string a, b;
    cin>>n;
    for(int i=0;i<n;i++)
    {
        cin>>a>>b;
        x = (a[0] - 'A') * 26 + a[1] - 'A';//不减的话数据太大,数组开不了那么大(map的优势) 
        y = (b[0] - 'A') * 26 + b[1] - 'A';
        if (x != y) {//x==y的话如果要配对的话只会配到自己省。题目说了不在同一省,所以要排除。
            city[x][y]++;
            ans += city[y][x];
        }
    }
    cout<<ans;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值