今天学了Trie,觉得自己要学的东西实在太多了
这一题的思路刘汝佳的书上有,一边插入一边计算,每个节点记录经过此处的字符串的数量,边插边加,计算的时候注意要考虑全面,注意插入字符串的第一个节点(比较时首字母就不同),中间的节点,和最后的节点(即字符串相同),看图:
例一的情况就是首字母不同:答案的计算过程就是1+2(节点0)
例二的情况就是中间的情况:答案的计算过程就是1+2+3(节点0)+2+4+6(节点1)+2+4+6(节点2)
例三的情况就是相等的情况(如3个ta) 那么答案的计算过程就是1+2(节点0)+2+4(节点1)+2+4(节点2)+1+2(\0)
因为这题SubmissionErr 因此不知道是否AC
于是去UVA论坛找了一组数据,看到答案对了,那就当AC了吧
样例在此:
57
ezysdylljqinvhbynmarolqkmcjsmglndhfdpyyezhmtamorrdjpksojtwjdmgdlbzsmfyzjfnaldaerqyijktlibcltbjotc
znjdbzwibamjjnectkikuem
vhnjumwoupuchhrdcoqqtlvqjmioemeobnspxqsjhckdpguzusyqpaxalovhdutazehjxyvbzvtrls
adzjwconawdwakeusgtojrrvfokjgv
qcuiubrepnfwjplpfesypqajuclmhjapnlpotlzhilstxiwmpyzvfylviqpdcpipkysglhxkwkmc
uryhqizjpipxvtvpbafrrcuoucygxadtymewqagkolsxkslajivkkcmebdonshgsnouiuqtzbso
yqaysywitrovhylkphryycadklcdrkncbbhesxysgqiuutjxlioclqnqhkvrxggcf
uahtcffpzsqxepgrqqmsfjpzzpeqmeaakijjbqknbkmhfhrgettxuwyqvurjtikyf
hggdikhnrjwozxzvyqcimhqbvkhcvmcwbfsnwmkgcwvlhdkdflbkrmakkltkuqvlxtqpa
kdxswqezemtympsoznotcajcvqbisypwilrvdxeyxzytvetgrcdabyovmzmqnnynunvekfaujvelrlrdhofxyqylneczqoyoh
chzfocdalgkqiwistnydvdstzt
brnaoqaysjsrktrpmlamgonzggcgpkobfcmtqqvxkquzqndqsomaysdnehyktocrhyzv
jjtbwcmobvbfdzbzbyyesqodmggzbuvtahqh
sxwjnocsvmphlwvlqzjylsmndwn
fjlegkzkmpwpnggufbdaepdecklbtppxvjshybmsscuxnaaoirobwyhevqjkjeijz
mlqclsrfxtgninqrwctiohnyxluvugiiiwtellrffjhfcmwomfoecoeaieiivnpcvmgrmtfljcbbfmajmregvvtx
bgtgungf
hzjaiepat
jdvcyqzjxypvpjyizqjudgkcavz
tcpdlyarkzongikuwgcdgbkxfvuoyoolyanliyfitkldbozhcmog
dnlnlbrgnofaiehovztmwadandicbeuxpzgloicfuowoplcikyayyputigg
jkprobeebosixkhpm
tfjedyegtszwqwivpmajbcliikoirhhzleziacsig
bgmjxazwmmbmnvloilbhqvzmlidtxakvzdyaohspbtrgchujejpazbmohv
amdmcnnscipjpoeegcinejrcnaqelmqeolmzxpfvb
ibpcbdrsavfuvchhfthfrxqxkmrpwtpzhnaxnmyndniikcrgnoadr
pyrybppwwarzeqrabmuqeh
flxeqjhepwphzoqnufjjnohywbvjzvtcldviwi
wcagqhjrmilakrshkkyothbzdptakukmkeyaprzbgecqfvcvuqzfbterntjxd
zcxgqvtzbgyqvewivyjssuydbalnielemwpjrqcgyrukcdnifkvrepktncirttswngybuhnuqxcvys
orhnmhtm
gbwffwwmnngcjreuivrndenlvyeoptujqnxbwrihugiyghvntenrr
zwcqnapjwhejveafavatrlhibpthzoebmlwlopdglbrfze
wpjyltiyfrtkatgmcwpdjqwvlfhbdorhhqpuzgjtvypcgnvljdpsyzaykfeyqzxwicopdkylgrumpolflh
ornorqomrbmjairpgrrvtclnqhjeljmmjqkvejqejcssmfuiyvrpuxd
iexpmtlfniqmlgrzhjuyflzyhcwbehatzydbnvyyrwwwkgbxvenvdvztkuxbjaa
qcbnqtpeeuwgrinrqjujhpwuckpfuyagkxvkzafgdbnxquiafpocjpfcmsupnphtqhg
jqfbloagblfkyoxmjdtbazjbsvokfrwbk
rlgukadtxoiguyyembskeemqqigewccpfdykpiptmovjjojfor
sijadtxdlbayhejoywutwoexeizmxffqznlpvsprfafnrynaechjlwmykqwpepmnjgajb
tfzjjgalldselqpuqxhidkgojvkxtijmubgqkjlpgdsvupxlujbspsifkjhzcmqismcwduitmuqhttzlwdzubqrhjslujndoinu
xevztpgmeydncepkildlmhdqyzjxxyrwqom
rbgspdmpagboyarijsruppbfkcwyrkzqfxkfnwvkbhwpxkjeqlteerotaodmdqbwvgdsqythjcmzfknclsxqpmpxm
fplpqhpmmezia
zgvjakoensuqggapaudujpejgdieepcxydfpxxpdqgddcl
yjxge
hrwsgefgribkioffxetghhufbjgnnvjviqdrjkdxsz
piewaxdpgtikxiripcgnvwjcwiidyvsjzugnwjqpfvsqptiavcbyaypgmhnvvswzbnnvufftgnfgtaigzybczgptjz
sl
zwgkwukiocujfkaanpyfwnadzixcczguiijy
lfolavnbbxwtpgjgaohlmkqzpqhxphdkmfhvnsibejszlkr
voycbavmexqgtzpgbmaqmwecqzbzqevlbvcwwkhctikekytmrifafwsiimfgqbbl
ihvnpjyrfqoocatgmdwtktjgsokczkeiwgpnldpnjnvlgxhdzgylv
nllftehdvoaesllxeowudchqkrmejbdwmrtkldnblrqbwptujhlumgjh
fvfsoltclvjdlzjawpgjijhyoaxoctyxogpsjmwjfotrbdpgamj
boddzjyrwibwydakziyvwbuxzgbozipggsjycyjbgpalguuqzh
sawsvtotnfegkjhmhkmzogudk
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;
const int maxn = 4000+10;
const int sigma_size = 26;
int ans;
struct Trie{
int ch[maxn][sigma_size];
int val[maxn];
int sz;
int flag[maxn];
Trie(){
sz = 1;
memset(ch[0],0,sizeof(ch[0]));
memset(flag,0,sizeof(flag));
}
int idx(const char c){
return c-'a';
}
void insert(const char *s){
int u = 0, n = strlen(s);
int ind = 0;
ans += val[0];
val[0]++;
for(int i = 0; i < n; i++){
// cout<<ans<<endl;
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz],0,sizeof(ch[sz]));
val[sz] = 0;
ch[u][c] = sz++;
}
u = ch[u][c];
ans += val[u]*2;
val[u]++;
}
ans += flag[u];
flag[u]++;
}
bool query(const char *s,const int v){
int n = strlen(s),u = 0;
for(int i = 0; i < n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
return false;
}
u = ch[u][c];
}
if(val[u]){
return true;
}
return false;
}
};
int main(){
int n,T=1;
Trie *TR;
while(~scanf("%d",&n) && n){
TR = new Trie();
ans = 0;
char st[1010];
for(int i = 0; i < n; i++){
cin >> st;
TR->insert(st);
}
printf("Case %d: %d\n",T++,ans);
}
return 0;
}