Doing this with others will also be misled. There will be a lot of people who have done this quickly, thinking that sorting and other things will come out, and then they will desperately Think About It, the tragedy is infinite.
It turned out to be the simplest dictionary tree;
Thought: Let's simply imagine that if we want to build all the things to be printed into a dictionary tree, if we want to delete all the last printed things, the number of times we want to walk on the tree is that all sides need to walk twice. But now, after the last printing, no Delete is required, that is to say, the last arrival of the leaf node does not need to be returned to the root node. Therefore, we can put the longest path to reach the leaves at the end (only once ).
# Include <string>
# Include <algorithm>
# Include <iostream>
Using namespace STD;
# Define max (A, B) A> B? A: B
# Define min (a, B) a <B? A: B
String s [10010];
Struct Node
{
Node * child [26];
// Int CNT;
Bool flag;
Node ()
{
Flag = true;
// CNT = 1;
For (INT I = 0; I <26; I ++)
Child [I] = NULL;
}
};
Node * root;
Int ans;
Void solve (string Str)
{
Int Len = Str. Length ();
Node * temp;
Temp = root;
Int I = 0, Pos = 0, index;
While (I <Len)
{
Index = STR [I]-'A ';
If (temp-> child [Index]! = NULL)
{
Temp = temp-> child [Index];
// Temp-> CNT ++;
}
Else
{
Temp-> child [Index] = new node ();
Temp = temp-> child [Index];
}
I ++;
}
}
Void finds (string Str)
{
Node * now;
Now = root;
Int I = 0, index, TOT = 0;
Int Len = Str. Length ();
While (I <Len)
{
Index = STR [I]-'A ';
/* If (now-> child [Index]-> CNT = 1)
{
Count ++;
}*/
If (now-> child [Index]-> flag)
{
TOT ++;
Now-> child [Index]-> flag = false;
}
Now = now-> child [Index];
I ++;
}
Ans + = tot * 2;
// Return count;
}
Int main ()
{
Int I, j, N, Len;
String STR;
While (CIN> N)
{
Int MA =-1, temp;
Ans = 0;
Root = new node ();
For (I = 0; I <n; I ++)
{
Cin> S [I];
Len = s [I]. Length ();
If (LEN> Ma)
MA = Len;
Ans ++;
Solve (s [I]);
}
For (I = 0; I <n; I ++)
{
Finds (s [I]);
// Ans + = temp;
// ANS-= (s [I]. Length ()-Temp );
}
Cout <ANS-MA <Endl;
}
Return 0;
}