<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
比的时候居然没注意到要用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;
}
}
}
}

本文探讨了一种解决多步骤姓名变更查询问题的算法。通过建立节点间的连接,该算法能够追踪每次更改前后的人名,最终输出初始和最终的人名序列。文章详细介绍了算法的实现过程,包括输入数据处理、节点连接维护以及最终结果输出。值得注意的是,文章指出在处理过程中忽略了数据结构的容量限制,导致出现运行错误,强调了在实际应用中注意细节的重要性。

456

被折叠的 条评论
为什么被折叠?



