锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Description Input Output Sample Input Sample Output
In the sample input below, "carbohydrate" can be abbreviated to
"carboh", but it cannot be abbreviated to "carbo" (or anything shorter)
because there are other words in the list that begin with "carbo".
An exact match will override a prefix match. For example, the
prefix "car" matches the given word "car" exactly. Therefore, it is
understood without ambiguity that "car" is an abbreviation for "car" ,
not for "carriage" or any of the other words in the list that begins
with "car". carbohydrate
cart
carburetor
caramel
caribou
carbonic
cartilage
carbon
carriage
carton
car
carbonatecarbohydrate carboh
cart cart
carburetor carbu
caramel cara
caribou cari
carbonic carboni
cartilage carti
carbon carbon
carriage carr
carton carto
car car
carbonate carbona
棰樻剰錛?br />鏌ユ壘鍗曡瘝鐨勬渶鐭墠緙
浠g爜濡備笅錛?br />
#include<string.h>
#define聽maxn聽10000
char聽dic[maxn][21];
#define聽M聽10000000
struct聽Dict
{
聽聽聽聽int聽times,聽isWord;
聽聽聽聽Dict聽*聽next[26];
};
Dict聽F[M];//渚沶ewNode鐢ㄧ殑錛屾瘮malloc蹇?/span>
int聽th聽=聽0;//th琛ㄧずF琚皟鐢ㄨ繃鐨勭┖闂?/span>
Dict聽*聽newNode()//
{
聽聽聽聽Dict聽*聽p聽=聽&F[th++];
聽聽聽聽p->isWord聽=聽0,聽p->times聽=聽0;//鍒濆鍖朠
聽聽聽聽for聽(int聽i聽=聽0;聽i聽<聽26;聽i++)
聽聽聽聽{
聽聽聽聽聽聽聽聽p->next[i]聽=聽NULL;
聽聽聽聽}
聽聽聽聽return聽p;
}
void聽insert(Dict聽*聽r,聽char聽*聽s)
{
聽聽聽聽if聽(s[0]聽==聽0)
聽聽聽聽{
聽聽聽聽聽聽聽聽r->isWord++;//琛ㄧず鍗曡瘝鏁伴噺
聽聽聽聽聽聽聽聽return;
聽聽聽聽}
聽聽聽聽if聽(!r->next[s[0]-'a'])//濡傛灉s[0]涓嶆槸緇撴潫絎︼紝涓旇鍗曡瘝鍓嶇紑涓嶅瓨鍦紝鐢熸垚褰撳墠瀛楁瘝
聽聽聽聽{
聽聽聽聽聽聽聽聽r->next[s[0]-'a']聽=聽newNode();
聽聽聽聽}
聽聽聽聽r->next[s[0]-'a']->times++;//璇ュ崟璇嶅綋鍓嶅墠緙鍑虹幇嬈℃暟
聽聽聽聽insert(r->next[s[0]-'a'],聽s聽+聽1);
}
//絎﹀悎鐨勫墠緙鏉′歡錛?br />//鍓嶇紑鏄敮涓鐨勶紝鎴栬呬互璇ュ崟璇嶆湰韜仛鑷繁鐨勫墠緙銆?/span>
int聽search(Dict聽*聽r,聽char聽*聽s,聽int聽len)
{
聽聽聽聽if聽(s[len]聽==聽0)
聽聽聽聽{
聽聽聽聽聽聽聽聽if聽(r->isWord)//琛ㄧず璇ュ墠緙鏄釜鍗曡瘝銆?/span>
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽return聽len;
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽return聽-1;
聽聽聽聽}
聽聽聽聽if聽(r->times聽==聽1)//琛ㄧず璇ュ墠緙鍞竴
聽聽聽聽{
聽聽聽聽聽聽聽聽return聽len;
聽聽聽聽}
聽聽聽聽if聽(!r->next[s[len]-'a'])
聽聽聽聽{
聽聽聽聽聽聽聽聽return聽-1;
聽聽聽聽}
聽聽聽聽return聽search(r->next[s[len]-'a'],聽s,聽len聽+聽1);
}
int聽main()
{
聽聽聽聽int聽i,聽j,聽k,聽t,聽len;
聽聽聽聽char聽a[21];
聽聽聽聽Dict聽*聽root聽=聽newNode();
聽聽聽聽for聽(i聽=聽0;聽scanf("%s",聽dic[i])聽!=聽EOF;聽i++)
聽聽聽聽{
聽聽聽聽聽聽聽聽insert(root,聽dic[i]);
聽聽聽聽}
聽聽聽聽for聽(j聽=聽0;聽j聽<聽i;聽j++)
聽聽聽聽{
聽聽聽聽聽聽聽聽printf("%s聽",聽dic[j]);
聽聽聽聽聽聽聽聽len聽=聽search(root,聽dic[j],聽0);
聽聽聽聽聽聽聽聽dic[j][len]聽=聽0;
聽聽聽聽聽聽聽聽printf("%s\n",聽dic[j]);
聽聽聽聽}
聽聽聽聽return聽0;
}
]]>