One day Polycarp published a funny picture in a social network making a poll about the color of his handle. Many of his friends started reposting Polycarp's joke to their news feed. Some of them reposted the reposts and so on.
These events are given as a sequence of strings "name1 reposted name2", where name1 is the name of the person who reposted the joke, and name2 is the name of the person from whose news feed the joke was reposted. It is guaranteed that for each string "name1 reposted name2" user "name1" didn't have the joke in his feed yet, and "name2" already had it in his feed by the moment of repost. Polycarp was registered as "Polycarp" and initially the joke was only in his feed.
Polycarp measures the popularity of the joke as the length of the largest repost chain. Print the popularity of Polycarp's joke.
The first line of the input contains integer n (1 ≤ n ≤ 200) — the number of reposts. Next follow the reposts in the order they were made. Each of them is written on a single line and looks as "name1 reposted name2". All the names in the input consist of lowercase or uppercase English letters and/or digits and have lengths from 2 to 24 characters, inclusive.
We know that the user names are case-insensitive, that is, two names that only differ in the letter case correspond to the same social network user.
Print a single integer — the maximum length of a repost chain.
5 tourist reposted Polycarp Petr reposted Tourist WJMZBMR reposted Petr sdya reposted wjmzbmr vepifanov reposted sdya
6
6 Mike reposted Polycarp Max reposted Polycarp EveryOne reposted Polycarp 111 reposted Polycarp VkCup reposted Polycarp Codeforces reposted Polycarp
2
1 SoMeStRaNgEgUe reposted PoLyCaRp
2
题意:输入一个n。输入一个人名a,reposted 和 另一个人名b,n行,第一行的人名b一定是polycarp(不区分大小写) 问人名传递的这个从polycarp开始的链的总长度。
思路:map一个容器s map<string,int > s 然后让字符串和字符串的出现时链长对应起来,后面每输入一组人名,就让前一个人名是后一个人名链长加一,并不断更新最长链长。最后输出最长链长。
在网上站下来了一点map的用法
1.
2.
3,map中元素的查找:
4,map中元素的删除:
5,map中 swap的用法:
}
6.map的sort问题:
}
7,
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<algorithm>
#include <map>
using namespace std;
//522A关于map
int main()
{
map<string,int> s;//map是c++的一个标准容器,提供了一对一关系,
s["polycarp"]=1;
int n;
cin>>n;
int res=1;
for(int i=0;i<n;i++)
{
string n1,n2;
cin>>n1>>n2>>n2;
for(int j=0;j<n1.length();j++)
{
n1[j]=tolower(n1[j]);
}
for(int j=0;j<n2.length();j++)
{
n2[j]=tolower(n2[j]);
}
s[n1]=s[n2]+1;
res=max(res,s[n1]);
}
cout<<res<<endl;
return 0;
}