codeforces #285 B题Misha and Changing Handles解题报告

13 篇文章 0 订阅
4 篇文章 0 订阅

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">         本题的意思是,给定多个改名的查询,每个查询包括一个新名字和旧名字,一个人可以多次更改,最终得到一个新名字,求这些查询中一共有多少个人,并且输出他最初的名字和最后的名字。(1<=q<=100)</span>

input
5
Misha ILoveCodeforces
Vasya Petrov
Petrov VasyaPetrov123
ILoveCodeforces MikeMirzayanov
Petya Ivanov
output
3
Petya Ivanov
Misha MikeMirzayanov
Vasya VasyaPetrov123
      考虑到q比较小,所以可以瞎搞。。直接对于每个节点设置sid和pid分别连接下一个名字和上一个名字,对于每一个查询,维护它的前驱和后继,最后便利一遍找到没有sid和没有pid的就是最初和最末的名字,最后根据循环找出每个初始名的结束名即可。

比的时候居然没注意到要用2*q存储,结果RE了,这点要注意。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct node{
	string v;
	int pid;
	int sid;
	void output(){
		cout<<v<<" "<<pid<<" "<<sid<<endl;
	}
}f[1001];
int main()
{
	freopen("in.txt","r",stdin);
	int n;
	while(cin>>n)
	{
		for(int i = 0; i < n ; i++)
		{
			f[i].pid = -1;
			f[i].sid = -1;
		}
		int cnt = 0;
		int tmp;
		for(int i = 0 ;i < n; i++)
		{
			string a,b;
			cin>>a>>b;
			int j;
			for(j = 0;j < cnt;j++)
				if(a==f[j].v) break;
			if(j==cnt)
			{
				
				f[cnt].v = a;
				f[cnt].pid = -1;
				f[cnt].sid = cnt + 1;
				cnt++;
				f[cnt].v = b;
				f[cnt].pid = cnt-1;
				f[cnt].sid = -1;
				cnt++;
			} else {
				
				f[cnt].v = b;
				f[cnt].pid = j;
				f[j].sid = cnt;
				f[cnt].sid = -1;
				cnt++;
			}
		}
//		for(int i = 0 ; i < cnt ;i++)
//		{
//			f[i].output();
//		}
		int ans = 0;
		for(int i = 0 ; i < cnt;i++)
		{
			if(f[i].pid == -1)
			{
				ans++;
				
			}
		}
		cout<<ans<<endl;
		for(int i = 0 ; i < cnt;i++)
		{
			if(f[i].pid == -1)
			{
				int si = f[i].sid;
				int pi ;
				while(si!=-1)
				{
					pi = si;
					si = f[si].sid;
					
				}
				cout<<f[i].v<<" "<<f[pi].v<<endl;
			}
		}		
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值