upc 單詞表 字典樹 dfs

2021-10-05 17:20:14 字數 2214 閱讀 3545

單詞表

時間限制: 1 sec 記憶體限制: 128 mb

題目描述

hzwer獲得了乙個n個單詞的單詞表,其中每個字元都是小寫字母,現在,他想和他的妹子研究一下這個單詞表。

設編號為u的單詞與編號為v的單詞(u 最近複習了下字典樹,讓後在巨巨的指導下補了這個題。

字典樹在統計字首字尾都比較好用。在構建字典樹的時候,每乙個點 idx 對應的 cnt [ idx ] 的數量即為以這個字母為結尾的公共字首個數。對於這個題來說,當 cnt [ idx ] > 1 的時候即符合題目中要求的條件。

直接考慮dfs爆搜一遍,每次列舉 a ~ z 字母,如果存在兒子並且 cnt [ idx ] > 1 那麼就可以擴充套件這個字首,使字首變得更長。

對於字尾是一樣處理,無非是把字串反過來,反向建一顆字典樹,正是由於反向建樹,所以原來要求的字典序最小,在求這個字尾的時候應該是字典序最大的。這個一開始沒想明白,dfs的時候從小到大列舉字母,以為貪心找字典序最小就不需要判斷長度相等的時候了,一直wa。所以

(1)長度變大的時候,理所當然的應該更新答案。

(2)長度與原來相等的時候,需要檢查一下是否可以更新。

最後還要求輸出單詞的對數,直接套公式:n * ( n - 1 ) / 2 。

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define x first

#define y second

using

namespace std;

typedef

long

long ll;

typedef pair<

int,

int> pii;

const

int n=

500010

,mod=

1e9+

7,inf=

0x3f3f3f3f

;const

double eps=

1e-6

;int n;

int son[2]

[n][26]

,cnt[2]

[n],idx[2]

;int dis[2]

,lc[2]

;char str[n]

,ans[2]

[n],tp[2]

[n];

void

insert

(int op,

char str)

}else}}

void

unit

(int len,

int op,

int u)

//更新答案

else

ans[op]

[len]

='\0';}

bool

check

(int len,

int op)

//檢查是否字典序更小

void

dfs(

int op,

int len,

int u)}if

(len>dis[op]

)unit

(len,op,u)

;else

if(len==dis[op]

&&check

(len,op)

)unit

(len,op,u);}

intmain()

dfs(0,

0,0)

;dfs(1

,0,0

);printf

("%s %lld\n"

,ans[0]

,1ll

*lc[0]

*(lc[0]

-1)/

2);printf

("%s %lld\n"

,ans[1]

,1ll

*lc[1]

*(lc[1]

-1)/

2);return0;

}/*5bbbaa

aacbb

bbdaa

aaaaa

bbcaa

*/

688 單詞表示數字

2017.10.26 三位三位的讀,然後再加thousand million billion等單位。再讀每乙個三位的時候,後兩位需要特殊處理。此外就是空格的增加需要格外注意一些,這主要是針對幾個相連的0出現的情況。public class solution int count 0 while num...

考研英語 單詞表1

precede 領先 predessor n.前輩 spectator n.觀眾 spectacular adj 壯觀的 atmosphere n.大氣層 beverage n,飲料 bizarre adj.稀奇古怪的 brass n,黃銅的,厚臉皮的 gossip.閒話,八卦 castle n,城...

程式設計實現在單詞表中查詢與已知單詞最接近的單詞

程式設計實現在單詞表中查詢與已知單詞最接近的單詞 1 如果單詞表中有要查詢的單詞輸出該單詞的位置 2 如果單詞表中沒有要查詢的單詞,輸出與要查詢的單詞最接近的單詞。最接近的單詞是指 兩個單詞僅有乙個字母不同,如 hello和hallo。include include using namespace ...