该算法类似2012考研计算机第42题
“假定采用带头结点的单链表保存单词,当两个单词有相同的后缀时,则可共享相同的后缀存储空间...”
思路:
得到两个链表长度,将较长者移动diff_len(长度差)个节点,最后同时向后搜索
遇到相同字母则计数,若遇到不相同字母则计数器清零。
#include <string.h> #include <stdio.h> typedef struct letter_node{ char letter; struct letter_node *next; }LETTER_NODE_T; void init_list(LETTER_NODE_T **p_list,const char *str){ const char *p = str; LETTER_NODE_T * list = NULL,*p_last = NULL; LETTER_NODE_T *p_node = NULL; if ( NULL == str){ return; } while(*p){ p_node = (LETTER_NODE_T *)malloc(sizeof(LETTER_NODE_T)); if ( NULL == p_node ){ printf("malloc error!\n"); return ; } p_node->next = NULL; p_node->letter = *p; if ( NULL == list ){ list = p_node; p_last = list; } else { p_last->next = p_node; p_last = p_node; } p++; } *p_list = list; } void free_list(LETTER_NODE_T *list) { LETTER_NODE_T *p = list,*tmp_p = NULL; while(p){ tmp_p = p; p = p->next; // printf("free:%p:%c\n",tmp_p,tmp_p->letter); free(tmp_p); } } void show_list(LETTER_NODE_T *list){ LETTER_NODE_T *p = list; while(p){ printf("%c",p->letter); if (p->next) printf("->"); p = p->next; } printf("\n"); } int list_length(LETTER_NODE_T *list){ LETTER_NODE_T *p = list; int length = 0; while(p){ length ++; p = p->next; } return length; } void find_common_str(LETTER_NODE_T *list1,LETTER_NODE_T *list2){ int diff_len = 0; //长度差 int len_cmp = 0; int common_len = 0; int len1 = list_length(list1); int len2 = list_length(list2); int i =0; LETTER_NODE_T *p = list1,*common_p1 = NULL; LETTER_NODE_T *q = list2,*common_p2 = NULL; if (len1 > len2){ len_cmp = 1; diff_len = len1-len2; for (i = 0;i < diff_len;i++) p= p->next; } else{ diff_len = len2 - len1; for (i = 0;i < diff_len;i++) q = q->next; } int count = 0; while(p && q){ printf("%d [%5c:%5c]\n",count++,p->letter,q->letter); if (p->letter == q->letter){ printf("letter:%c,common_len:%d\n",p->letter,common_len); if (!common_len){ common_p1 = p; common_p2 = q; } common_len ++; } else{ common_len = 0; } p = p->next; q = q->next; } printf("len1 = %d,len2 = %d,diff_len:%d,common_len = %d\n",len1,len2,diff_len,common_len); for (i = 0;i < common_len;i++){ printf("commont_letter%d = %c\n",i+1,common_p1->letter); common_p1 = common_p1->next; } } int main(int argc,char *argv[]) { LETTER_NODE_T * list1 = NULL; LETTER_NODE_T * list2 = NULL; printf("1111\n"); init_list(&list1,"lgdfgfgoading111111abcdefgccccdefghskdfksfyyyyyyyyyyyyyyccfdkfskdfdxtdfsdxxxxxxxxxyyyyyyyyzzzzzzzzz"); init_list(&list2,"ppppppppsdfkkjkjk111111111sdfajfjwejifjwebeginga11111111111abcdefgcccccdefgsdfdyyyyyyyyyyyyooppsdfuuufsdxyzxxxxxxxxxxyyyyyyyyzzzzzzzzz"); show_list(list1); show_list(list2); find_common_str(list1,list2); free_list(list1); free_list(list2); return 0; }
运行结果
linux:/myprogram/alg/list # ./find_common_str
1111
l->g->d->f->g->f->g->o->a->d->i->n->g->1->1->1->1->1->1->a->b->c->d->e->f->g->c->c->c->c->d->e->f->g->h->s->k->d->f->k->s->f->y->y->y->y->y->y->y->y->y->y->y->y->y->y->c->c->f->d->k->f->s->k->d->f->d->x->t->d->f->s->d->x->x->x->x->x->x->x->x->x->y->y->y->y->y->y->y->y->z->z->z->z->z->z->z->z->z
p->p->p->p->p->p->p->p->s->d->f->k->k->j->k->j->k->1->1->1->1->1->1->1->1->1->s->d->f->a->j->f->j->w->e->j->i->f->j->w->e->b->e->g->i->n->g->a->1->1->1->1->1->1->1->1->1->1->1->a->b->c->d->e->f->g->c->c->c->c->c->d->e->f->g->s->d->f->d->y->y->y->y->y->y->y->y->y->y->y->y->o->o->p->p->s->d->f->u->u->u->f->s->d->x->y->z->x->x->x->x->x->x->x->x->x->x->y->y->y->y->y->y->y->y->z->z->z->z->z->z->z->z->z
0 [ l: j]
1 [ g: i]
2 [ d: f]
3 [ f: j]
4 [ g: w]
5 [ f: e]
6 [ g: b]
7 [ o: e]
8 [ a: g]
9 [ d: i]
10 [ i: n]
11 [ n: g]
12 [ g: a]
13 [ 1: 1]
letter:1,common_len:0
14 [ 1: 1]
letter:1,common_len:1
15 [ 1: 1]
letter:1,common_len:2
16 [ 1: 1]
letter:1,common_len:3
17 [ 1: 1]
letter:1,common_len:4
18 [ 1: 1]
letter:1,common_len:5
19 [ a: 1]
20 [ b: 1]
21 [ c: 1]
22 [ d: 1]
23 [ e: 1]
24 [ f: a]
25 [ g: b]
26 [ c: c]
letter:c,common_len:0
27 [ c: d]
28 [ c: e]
29 [ c: f]
30 [ d: g]
31 [ e: c]
32 [ f: c]
33 [ g: c]
34 [ h: c]
35 [ s: c]
36 [ k: d]
37 [ d: e]
38 [ f: f]
letter:f,common_len:0
39 [ k: g]
40 [ s: s]
letter:s,common_len:0
41 [ f: d]
42 [ y: f]
43 [ y: d]
44 [ y: y]
letter:y,common_len:0
45 [ y: y]
letter:y,common_len:1
46 [ y: y]
letter:y,common_len:2
47 [ y: y]
letter:y,common_len:3
48 [ y: y]
letter:y,common_len:4
49 [ y: y]
letter:y,common_len:5
50 [ y: y]
letter:y,common_len:6
51 [ y: y]
letter:y,common_len:7
52 [ y: y]
letter:y,common_len:8
53 [ y: y]
letter:y,common_len:9
54 [ y: y]
letter:y,common_len:10
55 [ y: y]
letter:y,common_len:11
56 [ c: o]
57 [ c: o]
58 [ f: p]
59 [ d: p]
60 [ k: s]
61 [ f: d]
62 [ s: f]
63 [ k: u]
64 [ d: u]
65 [ f: u]
66 [ d: f]
67 [ x: s]
68 [ t: d]
69 [ d: x]
70 [ f: y]
71 [ s: z]
72 [ d: x]
73 [ x: x]
letter:x,common_len:0
74 [ x: x]
letter:x,common_len:1
75 [ x: x]
letter:x,common_len:2
76 [ x: x]
letter:x,common_len:3
77 [ x: x]
letter:x,common_len:4
78 [ x: x]
letter:x,common_len:5
79 [ x: x]
letter:x,common_len:6
80 [ x: x]
letter:x,common_len:7
81 [ x: x]
letter:x,common_len:8
82 [ y: y]
letter:y,common_len:9
83 [ y: y]
letter:y,common_len:10
84 [ y: y]
letter:y,common_len:11
85 [ y: y]
letter:y,common_len:12
86 [ y: y]
letter:y,common_len:13
87 [ y: y]
letter:y,common_len:14
88 [ y: y]
letter:y,common_len:15
89 [ y: y]
letter:y,common_len:16
90 [ z: z]
letter:z,common_len:17
91 [ z: z]
letter:z,common_len:18
92 [ z: z]
letter:z,common_len:19
93 [ z: z]
letter:z,common_len:20
94 [ z: z]
letter:z,common_len:21
95 [ z: z]
letter:z,common_len:22
96 [ z: z]
letter:z,common_len:23
97 [ z: z]
letter:z,common_len:24
98 [ z: z]
letter:z,common_len:25
len1 = 99,len2 = 134,diff_len:35,common_len = 26
commont_letter1 = x
commont_letter2 = x
commont_letter3 = x
commont_letter4 = x
commont_letter5 = x
commont_letter6 = x
commont_letter7 = x
commont_letter8 = x
commont_letter9 = x
commont_letter10 = y
commont_letter11 = y
commont_letter12 = y
commont_letter13 = y
commont_letter14 = y
commont_letter15 = y
commont_letter16 = y
commont_letter17 = y
commont_letter18 = z
commont_letter19 = z
commont_letter20 = z
commont_letter21 = z
commont_letter22 = z
commont_letter23 = z
commont_letter24 = z
commont_letter25 = z
commont_letter26 = z