洛谷P3405 [USACO16DEC]Cities and States省市
P3405 [USACO16DEC]Cities and States省市
題目描述
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!
為了讓奶牛在智力上受到刺激,農(nóng)夫約翰在谷倉的墻上放了一張美國地圖。由于奶牛在谷倉里花了很多時(shí)間盯著這張地圖,他們開始注意到一些奇怪的關(guān)系。例如,城市Flint,在MI省,或者M(jìn)iami在FL省,他們有一種特殊的關(guān)系:“Flint”市前兩個(gè)字母就是“FL”省,邁阿密前兩個(gè)字母是“MI”省。
讓我們說,兩個(gè)城市是一個(gè)“特殊的一對”,如果他們滿足這個(gè)屬性,來自不同的省。奶牛想知道有多少特殊的城市存在。請幫助他們解決這個(gè)有趣的地理難題!
輸入輸出格式
輸入格式:?
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.
輸入的第一行包含(),地圖上的城市數(shù)量。
下一行包含兩個(gè)字符串:一個(gè)城市的名稱(字符串至少2個(gè)最多10個(gè)大寫字母),和它的兩個(gè)字母的州代碼(一串2個(gè)大寫字母)。注意狀態(tài)代碼可能像ZQ,這并不是一個(gè)真正的美國。同一名稱的多個(gè)城市可以存在,但它們將處于不同的州。
?
輸出格式:?
Please output the number of special pairs of cities.
請輸出特殊城市對數(shù)。
?
輸入輸出樣例
輸入樣例#1:6 MIAMI FL DALLAS TX FLINT MI CLEMSON SC BOSTON MA ORLANDO FL 輸出樣例#1:
1 /*樣例可以直接看成下面這個(gè)樣子的:MIFLDATXFLMICLSCBOMAORFL如果有滿足題意,那么就是MIFL 和 FLMI,那么,這里的有效數(shù)據(jù)只有四個(gè)字母,那么每一行我們就可以看作一個(gè)四位26進(jìn)制數(shù)!!維護(hù)兩個(gè)數(shù)組,一個(gè)hash1[456976],一個(gè)hash1[456976],具體456976是什么意思,是因?yàn)槿绻覀儗看做0,B看做1……Z看做25,那么最小的數(shù)AAAA=0,就是最大的數(shù)就是ZZZZ=((25*26+25)*26+25)*26+25=456975。那么,問題輕易解出——hash1表示原來的數(shù),hash2表示市編號和州編號翻轉(zhuǎn)后的表示的數(shù)。然后循環(huán)一遍,算出答案。注意此時(shí)算出的答案是兩倍的,因?yàn)槊總€(gè)算了兩遍。 */ #include<iostream> #include<cstdio> using namespace std; #define maxn 26*26*26*27 int n,h1[maxn],h2[maxn]; char s1[100],s2[100]; int main(){scanf("%d",&n);for(int i=1;i<=n;i++){scanf("%s%s",s1+1,s2+1);if(s1[1]!=s2[1]||s1[2]!=s2[2]){int x1=(((s1[1]-'A')*26+s1[2]-'A')*26+s2[1]-'A')*26+s2[2]-'A';int x2=(((s2[1]-'A')*26+s2[2]-'A')*26+s1[1]-'A')*26+s1[2]-'A';h1[x1]++;h2[x2]++;}}long long ans=0;for(int i=0;i<maxn;i++)ans+=h1[i]*h2[i];ans/=2;cout<<ans; }
?
轉(zhuǎn)載于:https://www.cnblogs.com/thmyl/p/7512154.html
總結(jié)
以上是生活随笔為你收集整理的洛谷P3405 [USACO16DEC]Cities and States省市的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 计算机无线网络无法连接网络,笔记本连无线
- 下一篇: CAD将图形输出成png图片的三种方法