我自己写的有点小问题,先贴上自己的,日后找找问题。
底下一版参考柳婼大神:https://blog.csdn.net/liuchuo/article/details/52144527?utm_source=blogxgwz9
#include<iostream>
#include<vector>
using namespace std;
struct letter{
char c;
int next;
}L[100000];
struct element{
char c;
int index;
};
vector<element> w1,w2;
int main(){
int ad1,ad2,N,i,ad,j;
cin>>ad1>>ad2>>N;
for(i=0;i<N;i++){
cin>>ad;
cin>>L[ad].c>>L[ad].next;
}
//cout<<ad1<<"******"<<ad2<<endl;
while(ad1!=-1){
element e;
e.c=L[ad1].c;
e.index=ad1;
w1.push_back(e);
ad1=L[ad1].next;
}
while(ad2!=-1){
element e;
e.c=L[ad2].c;
e.index=ad2;
w2.push_back(e);
ad2=L[ad2].next;
}
int cnt=0;
i=w1.size()-1;
j=w2.size()-1;
//cout<<"i:"<<i<<"j:"<<j<<endl;
for(;w1[i].c==w2[j].c &&i>=0&&j>=0;i--,j--)
cnt++;
if(cnt==0)
cout<<"-1";
else
printf("%05d",w1[i+1].index);
system("pause");
return 0;
}
#include<iostream>
using namespace std;
struct letter{
char c;
int next;
bool flag;
}L[100000];
int main(){
int ad1,ad2,N,i,ad;
cin>>ad1>>ad2>>N;
for(i=0;i<N;i++){
cin>>ad;
cin>>L[ad].c>>L[ad].next;
L[ad].flag=false;
}
//cout<<ad1<<"******"<<ad2<<endl;
while(ad1!=-1){
L[ad1].flag=true;
ad1=L[ad1].next;
}
while(ad2!=-1){
if(L[ad2].flag==true){
printf("%05d",ad2);
return 0;
}
ad2=L[ad2].next;
}
printf("-1");
return 0;
}