PAT Advanced 1032. Sharing (25) (C語(yǔ)言實(shí)現(xiàn))

我的PAT系列文章更新重心已移至Github痘系,歡迎來(lái)看PAT題解的小伙伴請(qǐng)到Github Pages瀏覽最新內(nèi)容(本篇文章鏈接)飒箭。此處文章目前已更新至與Github Pages同步。歡迎star我的repo锡溯。

題目

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.

fig.jpg

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 ( \le 10^5 ), 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

whereAddress 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 00010

Sample Output 1:

67890

Sample Input 2:

00001 00002 4
00001 a 10001
10001 s -1
00002 a 10002
10002 t -1

Sample Output 2:

-1

思路

一個(gè)鏈表相關(guān)的題目。

依然暴力地(再次說(shuō)明杰刽,很不實(shí)用。不過(guò)最壞情況就需要這么多艺蝴,所以對(duì)于OJ來(lái)說(shuō),不算浪費(fèi))申請(qǐng)一個(gè)100000長(zhǎng)度的數(shù)組鸟废,直接用數(shù)組索引作為鏈表地址猜敢。

思路很簡(jiǎn)單:

記錄鏈表:
    結(jié)構(gòu)數(shù)組[Address] => 結(jié)構(gòu)數(shù)組[Next]
遍歷第一個(gè)單詞:
    標(biāo)記所有遍歷的結(jié)點(diǎn)
遍歷第二個(gè)單詞:
    輸出第一個(gè)被標(biāo)記的結(jié)點(diǎn)

我定義的結(jié)構(gòu)只有下一個(gè)結(jié)點(diǎn)的地址和一個(gè)標(biāo)記,沒有記錄字符盒延,因?yàn)槟銜?huì)發(fā)現(xiàn)它沒用缩擂。。兰英。

盲點(diǎn):?jiǎn)卧~開頭地址可能為NULL撇叁。盲生,你是否發(fā)現(xiàn)這個(gè)了華點(diǎn)畦贸?

代碼

最新代碼@github陨闹,歡迎交流

#include <stdio.h>

typedef struct Node Node;
struct Node {
    int checked;
    struct Node *next;
};

int main()
{
    char data;
    int start1, start2, address, next, N;
    Node list[100000] = {0}, *p;

    scanf("%d %d %d", &start1, &start2, &N);
    /* record a linked list */
    for(int i = 0; i < N; i++)
    {
        scanf("%d %c %d", &address, &data, &next);
        list[address].next = next == -1 ? NULL : &list[next];
    }

    if(start1 == -1 || start2 == -1)
    {
        printf("-1");
        return 0;
    }
    /* First traverse the first string */
    for(p = list + start1; p; p = p->next)
        p->checked = 1;
    /* Then traverse the second looking for checked node */
    for(p = list + start2; p && !p->checked; p = p->next)
        ;

    if(p)
        printf("%05ld", p - list);
    else
        printf("-1");

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末楞捂,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子趋厉,更是在濱河造成了極大的恐慌寨闹,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,198評(píng)論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件君账,死亡現(xiàn)場(chǎng)離奇詭異繁堡,居然都是意外死亡,警方通過(guò)查閱死者的電腦和手機(jī)乡数,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評(píng)論 3 398
  • 文/潘曉璐 我一進(jìn)店門椭蹄,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái),“玉大人净赴,你說(shuō)我怎么就攤上這事绳矩。” “怎么了玖翅?”我有些...
    開封第一講書人閱讀 167,643評(píng)論 0 360
  • 文/不壞的土叔 我叫張陵翼馆,是天一觀的道長(zhǎng)。 經(jīng)常有香客問(wèn)我金度,道長(zhǎng)应媚,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,495評(píng)論 1 296
  • 正文 為了忘掉前任猜极,我火速辦了婚禮中姜,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘魔吐。我一直安慰自己扎筒,他們只是感情好奥溺,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,502評(píng)論 6 397
  • 文/花漫 我一把揭開白布方灾。 她就那樣靜靜地躺著痛单,像睡著了一般挥吵。 火紅的嫁衣襯著肌膚如雪脉幢。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,156評(píng)論 1 308
  • 那天,我揣著相機(jī)與錄音凑保,去河邊找鬼冈爹。 笑死,一個(gè)胖子當(dāng)著我的面吹牛欧引,可吹牛的內(nèi)容都是我干的频伤。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評(píng)論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼芝此,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼憋肖!你這毒婦竟也來(lái)了因痛?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,659評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤瞬哼,失蹤者是張志新(化名)和其女友劉穎婚肆,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體坐慰,經(jīng)...
    沈念sama閱讀 46,200評(píng)論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡较性,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,282評(píng)論 3 340
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了结胀。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片赞咙。...
    茶點(diǎn)故事閱讀 40,424評(píng)論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖糟港,靈堂內(nèi)的尸體忽然破棺而出攀操,到底是詐尸還是另有隱情,我是刑警寧澤秸抚,帶...
    沈念sama閱讀 36,107評(píng)論 5 349
  • 正文 年R本政府宣布速和,位于F島的核電站,受9級(jí)特大地震影響剥汤,放射性物質(zhì)發(fā)生泄漏颠放。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,789評(píng)論 3 333
  • 文/蒙蒙 一吭敢、第九天 我趴在偏房一處隱蔽的房頂上張望碰凶。 院中可真熱鬧,春花似錦鹿驼、人聲如沸欲低。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評(píng)論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)砾莱。三九已至,卻和暖如春凄鼻,著一層夾襖步出監(jiān)牢的瞬間恤磷,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評(píng)論 1 271
  • 我被黑心中介騙來(lái)泰國(guó)打工野宜, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人魔策。 一個(gè)月前我還...
    沈念sama閱讀 48,798評(píng)論 3 376
  • 正文 我出身青樓匈子,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親闯袒。 傳聞我的和親對(duì)象是個(gè)殘疾皇子虎敦,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,435評(píng)論 2 359