To store English words, one method is to use linked lists and store a word letter by letter. To save some space, we may let the words share the same sublist if they share the same suffix. For example, "loading" and "being" are stored as showed in Figure 1.
Figure 1
You are supposed to find the starting position of the common suffix (e.g. the position of "i" in Figure 1).
Input Specification:
Each input file contains one test case. For each case, the first line contains two addresses of nodes and a positive N (<= 105), where the two addresses are the addresses of the first nodes of the two words, and N is the total number of nodes. The address of a node is a 5-digit positive integer, and NULL is represented by -1.
Then N lines follow, each describes a node in the format:
Address Data Next
where Address is the position of the node, Data is the letter contained by this node which is an English letter chosen from {a-z, A-Z}, and Next is the position of the next node.
Output Specification:
For each case, simply output the 5-digit starting position of the common suffix. If the two words have no common suffix, output "-1" instead.
Sample Input 1:11111 22222 9 67890 i 00002 00010 a 12345 00003 g -1 12345 D 67890 00002 n 00003 22222 B 23456 11111 L 00001 23456 e 67890 00001 o 00010Sample Output 1:
67890Sample Input 2:
00001 00002 4 00001 a 10001 10001 s -1 00002 a 10002 10002 t -1Sample Output 2:
-1
用真实链表模拟。后来看了算法笔记用静态链表写。
注意:地址不同可能字符相同,如样例2 ,00001和00002,都是a。说白了这题就是找重复的结点地址,而不是重复的字符。字符并没什么卵用。
方法一就错了,找了重复的字符。
#include<stdio.h>
#include<stdlib.h>
typedef struct Node *node;
struct Node{
int address;
char c;
node next;
}stu[100001];
int map[128];
int main(){
int n,i,start1,start2,address,next;
char data;
scanf("%d %d %d",&start1,&start2,&n);
for(i=0;i<n;i++){
scanf("%d %c %d",&address,&data,&next);
stu[address].address=address;
stu[address].c=data;
if(next==-1){
stu[address].next=NULL;
}
else{
stu[address].next=&stu[next];
}
}
node head1=(node)malloc(sizeof(struct Node));
node head2=(node)malloc(sizeof(struct Node));
head1->next=&stu[start1];
head2->next=&stu[start2];
node L1=head1;
node L2=head2;
head1=head1->next;
head2=head2->next;
int index=-1;
while(head1){
if(map[head1->c]==0){
map[head1->c]=1;
}
head1=head1->next;
}
while(head2){
if(map[head2->c]==1){
index=head2->address;break;//找重复字符没什么用!!
}
head2=head2->next;
}
if(index!=-1){
printf("%05d",index);
}
else{
printf("%d",-1);
}
}
方法二:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node *node;
struct Node{
int address;
char c;
node next;
}stu[100001];
int map[100001];
int main(){
int n,i,start1,start2,address,next;
char data;
scanf("%d %d %d",&start1,&start2,&n);
for(i=0;i<n;i++){
scanf("%d %c %d",&address,&data,&next);
stu[address].address=address;
// stu[address].c=data;
if(next==-1){
stu[address].next=NULL;
}
else{
stu[address].next=&stu[next];
}
}
node head1=(node)malloc(sizeof(struct Node));
node head2=(node)malloc(sizeof(struct Node));
head1->next=&stu[start1];
head2->next=&stu[start2];
node L1=head1;
node L2=head2;
head1=head1->next;
head2=head2->next;
int index=-1;
while(head1){
if(map[head1->address]==0){
map[head1->address]=1;
}
head1=head1->next;
}
while(head2){
if(map[head2->address]==1){
index=head2->address;break;
}
head2=head2->next;
}
if(index!=-1){
printf("%05d",index);
}
else{
printf("%d",-1);
}
}
方法三:静态链表
#include<stdio.h>
struct node{
int next;
}stu[100010];
int flag[100010]={0};
int main(){
int start1,start2,n,i,s,e;
char c;
scanf("%d %d %d",&start1,&start2,&n);
for(i=0;i<n;i++){
scanf("%d %c %d",&s,&c,&e);
stu[s].next=e;
}
while(start1!=-1){
flag[start1]=1;
start1=stu[start1].next;
}
int index=-1;
while(start2!=-1){
if(flag[start2]==1){
index=start2;break;
}
start2=stu[start2].next;
}
if(index!=-1){
printf("%05d",index);
}
else{
printf("-1");
}
}