Title:
Enter the starting address of two words and a positive integer N (<=1e5), and then enter N lines of data, each line includes a five-digit letter address, The address of the letter and the next letter. Output the address of the first letter of the common suffix of these two words, or -1 if there is no common suffix.
Code:
#define HAVE_STRUCT_TIMESPEC
#include
using namespace std;
< span style="font-family:'Microsoft YaHei'; font-size: 18px;">string a1,a2;
int n;
string s,nex;
< span style="font-family:'Microsoft YaHei'; font-size: 18px;">char x;
map
map
int main(){
cin>>a1>>a2>>n;
for(int i=1;i<=n;++i){
cin>>s>>x>>nex;
mp[s] =nex;
}
int cnt=0;
while(++ cnt){
if(cnt&1&&a1.size()==5){
++m[a1];
if(m[a1]==2){
cout <
return 0;
}
a1=mp[a1];
}
else if(!(cnt&1)&&a2.size( )==5){
++m[a2];
< span style="font-family:'Microsoft YaHei'; font-size: 18px;"> if(m[a2]==2){
cout<
return 0; span>
}
a2=mp[a2];
}
if(a1.size()==2&&a2.size()==2){//Don’t use a1.size()==a2.size()==2, Although some samples may be output as desired, they are actually far from the desired effect. In this way, first compare the size of a1 and the size of a2. If they are equal to 1, they are equal to 0, and they will never be equal to 2< /span>
cout<<-1;
break;
}< /span>
}
return 0;
}
< /p>