VK Cup 2015 - Qualification Round 1 A. Reposts(树)

传送门

Description

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 "name1reposted 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.

Input

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.

Output

Print a single integer — the maximum length of a repost chain.

Sample Input

5
tourist reposted Polycarp
Petr reposted Tourist
WJMZBMR reposted Petr
sdya reposted wjmzbmr
vepifanov reposted sdya

6
Mike reposted Polycarp
Max reposted Polycarp
EveryOne reposted Polycarp
111 reposted Polycarp
VkCup reposted Polycarp
Codeforces reposted Polycarp

1
SoMeStRaNgEgUe reposted PoLyCaRp

Sample Output

6

2

2

思路

题解:

 

 

#include<bits/stdc++.h>
using namespace std;
const int maxn = 205;
char str[maxn],a[maxn],b[maxn];

int main(){
	int n,res = 0;
	map<string,string>mp;
	set<string>s;
	set<string>::iterator it;
	scanf("%d",&n);
	getchar();
	for (int i = 0;i < n;i++){
		gets(str);

		int j = 0,len = strlen(str);
		for (j = 0;str[j] != ' ';j++){
			if (str[j] >= 65 && str[j] <= 90){
				str[j] += 32;
			}
		}
		strncpy(a,str,j);
		for (j = len - 1; str[j] != ' ';j--){
			if (str[j] >= 65 && str[j] <= 90){
				str[j] += 32;
			}
		}
		strncpy(b,str + j + 1,len - 1 - j);

		s.insert(a),s.insert(b);
		mp[a] = b;
		memset(a,0,sizeof(a));
		memset(b,0,sizeof(b));
	}

	for (it = s.begin();it != s.end();it++){
		int len = 1;
		string val = *it;
		while (mp.count(val))	val = mp[val],len++;
		res = max(res,len);
	}
	printf("%d\n",res);
	return 0;
}

  

转载于:https://www.cnblogs.com/ZhaoxiCheung/p/7309589.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值