#include<iostream>
#include<string>
#include<map>
#include<vector>
using namespace std;
char word[100005];
int node[100005];
int main()
{
int firadd1,firadd2,n;
cin>>firadd1>>firadd2>>n;
int add,next;
char c;
map<char,int> kkk;
for(int i=0;i<n;i++)
{
cin>>add>>c>>next;
node[add]=next;
word[add]=c;
kkk[c]=add;
}
vector<char> word1,word2;
int start=firadd1;
while(start!=-1)
{
word1.push_back(word[start]);
start=node[start];
}
start=firadd2;
while(start!=-1)
{
word2.push_back(word[start]);
start=node[start];
}
int same=0;
int end1=word1.size()-1;
int end2=word2.size()-1;
while(word1[end1]==word2[end2])
{
same++;
end1--;
end2--;
if(end1<0||end2<0)
break;
}
if(same==0)
cout<<"-1";
else
printf("%05d",kkk[word1[end1+1]]);
return 0;
}
还有段错误 也搞不清为什么。。。
不过说真的,静态链表的写法真的惊到我了,原来这题可以写这么简单。菜是原罪。
#include<iostream>
using namespace std;
struct {
bool used = false;
int next = -2;
}num[100000];
int main()
{
int h1, h2, n, i, temp1, temp2;
char c;
cin >> h1 >> h2 >> n;
for (i = 0; i < n; i++)
{
cin >> temp1 >> c >> temp2;
num[temp1].next = temp2;
}
while (h1 != -1)
{
num[h1].used = true;
h1 = num[h1].next;
}
while (h2 != -1)
{
if (num[h2].used)
{
printf("%05d", h2);
return 0;
}
h2 = num[h2].next;
}
if (h2 == -1)
cout << "-1";
return 0;
}