问题 问答题

假定采用带头结点的单链表保存单词,当两个单词有相同的后缀时,则可共享相同的后缀存储空间。例如,“loading”和“being”的存储映像如下图所示。

设str1和str2分别指向两个单词所在单链表的头结点,链表结点结构为,请设计一个时间上尽可能高效的算法,找出由str1和str2所指的两个链表共同后缀的起始位置(如图中字符i所在结点的位置p)。要求:

根据设计思想,采用C或C++或Java语言描述算法,关键之处给出注释。

答案

参考答案:

算法实现: typedef struct Node{ char data; struct Node*next; }SNODE; SNODE*findlist(SNODE*str1,SNODE*str2){ int m,n; SNODE*P,*q; m=listlen(str1); /*求str1的长度。O(m)*/ n=listlen(str2); /*求str2的长度。O(n)*/ /*以下3个循环的时间复杂度为:O(max(m,n))*/ for(P=str1;m>n;m--) /*使P指向的链表与q指向的链表等长*/ P=P->next; for(q=str2;m<n;n--) /*使q指向的链表与P指向的链表等长*/ q=q->next; while(p->next!=NULL&&p->next!=q->next){ /*查找共同后缀起始点*/ p=p->next; /*两个指针同步向后移动*/ q=q->next; } return p->next; /*返回共同后缀的起始点*/ } int listlen(SNODE*head){ /*求链表长度*/ int len=0; while(head->next!=NULL){ len++; head=head->next; } return len; }

填空题
填空题


Statistics from China (1) be mind boggling: 1.2 billion (2) , 1.73 trillion cigarettes smoked in a year, 7,000 different (3) of woody plants. But amid all of these staggering sums, one factoid stands (4) for both its audacious size and for what it says about China’s future: there are 630 million Chinese under the age of 24. That’s a lot of (5) energy to burn. Materialism may be the (6) preoccupation among China’s young people these days, but just beneath the surface lies a feeling (7) wounded nationalist pride and an ever-deepening spiritual hunger. It isn’t clear where China’s young people are headed. But this is a generation that, by its (8) size and certain talents, will (9) the world’s destiny.
Here’s another sobering statistic: this is Terry McCarthy’s 22nd, and final, cover (10) for TIME Asia-he’s leaving the region (11) three years to become TIME’s Los Angeles (12) chief. McCarthy, who has indefatigably crisscrossed Asia out of his twin bases of Hong Kong and Shanghai, was the main driver (13) this week’s superb special report on young China. He developed the story list, guided much (14) the reporting
and wrote some of the articles. All (15) planning a swank black-tie masked ball last weekend in Shanghai. "I was (16) by the willingness of individual Chinese to write for us or talk about their (17) ," McCarthy says about the special (18) . "These are the guys (19) are going to be running the country in 20 (20) ." If we’re lucky, McCarthy will be back in the region long before then.