Start with the question:
Strcmp () anyone?
Time limit:2000 ms
Memory limit:0 KB
64bit Io format:% LLD & % llusubmit status
Description
J 
? Strcmp ()? Anyone? Input:Standard Input Output:Standard output 
Strcmp () is a library function in C/C ++ which compares two strings. it takes two strings as input parameter and decides which one is lexicographically larger or smaller: if the first string is greater then it returns a positive value, if the second string is greater it returns a negative value and if two strings are equal it returns a zero. the code that is used to compare two strings in C/C ++ library is shown below:
IntStrcmp (Char* S,Char* T) { IntI; For(I = 0; s [I] = T [I]; I ++) If (s [I] = '\ 0 ') Return0; ReturnS [I]T [I]; } 
Figure: The standard strcmp () Code provided for this problem. 
The number of comparisons required to compare two strings in strcmp () function is never returned by the function. but for this problem you will have to do just that at a larger scale. strcmp () function continues to compare characters in the same position of the two strings until two different characters are found or both strings come to an end. of course it assumes that last character of a string I S a null (? \ 0 ?) Character. For example the table below shows what happens when? Than? And? That ?; ? There? And? The? Are compared using strcmp () function. To understand how 7 comparisons are needed in both cases please consult the code block given above.
T 
H 
A 
N 
\ 0 

T 
H 
E 
R 
E 
\ 0 

= 
= 
= 
= 

= 
= 
= 
= 


T 
H 
A 
T 
\ 0 
T 
H 
E 
\ 0 


Returns negative value 7. Comparisons 
Returns positive value 7. Comparisons 
Input
The input file contains maximum 10 sets of inputs. The description of each set is given below:
Each set starts with an integer N (0 <n <4001) which denotes the total number of strings. Each of the next n lines contains one string. Strings contain only alphanumerals (? 0 ?? ? 9 ?, ? A ?? ? Z ?, ? A ?? ? Z ?) Have a maximum length of 1000, and a minimum length of 1.
Input is terminated by a line containing a single zero. input file size is around 23 MB.
Output
For each set of input produce one line of output. this line contains the serial of output followed by an integer T. this T Denotes the total number of comparisons that are required in the strcmp () function if all the strings are compared with one another exactly once. so for N strings the function strcmp () will be called exactly times. you have to calculate total number of comparisons inside the strcmp () function in those CILS. you can assume that the value of T will fit safely in a 64bit signed integer. please note that the most straightforward solution (worst case complexity O (n2 * 1000) will time out for this problem.
Sample input output for sample input
2 A B 4 Cat Hat Mat Sir 0 
Case 1: 1 Case 2: 6 
Problem setter: Shahriar Manzoor, special thanks: Md. arifuzzaman Arif, Soel Hafiz, manzurur Rahman Khan
Question: How many times do I need to compare n strings of no more than 1000 characters based on the formula given.
Apparently, the bruteforce match is not feasible. Here, we create a trie tree to save the string, and then count the number of times to be determined each time the string is inserted. After analysis, we can find that for two strings with the same prefix, the number of times to be compared is equal to the prefix length * 2 + A comparison of the one that does not match after the prefix. That is to say, if two strings A, B, A = abcaa, B = abcbb, then we need to compare them seven times in total. If the two strings are identical, the number of times we need to compare is the length of a string * 2. The length here is the terminator '\ 0', because this position also needs to be determined.
So how can we get the correct results. We can find that if a string has been inserted, When I insert a new string, we need to compare the same bit at the same position twice, for different characters at the same position, we only compare them once. At the same time, we find that all strings must be compared at least once. So we can design the code like this: When we access the same character at the same position, we add 2 for every pair of identical characters. For different characters, so we know that the comparison will only end after one comparison (for the two strings), we can add these times at the end, because for the whole, these times will not change. When we compare two identical strings, when we compare the Terminator, we only add one, so that we can add the remaining one to the overall processing. (If you do not understand it, You can manually simulate it once based on the Code ).
With the idea, the code is implemented. At first, I used a twodimensional array of 4000*1000*62 to implement the trie tree, but it timed out, I think the main reason is that initialization is required every time a new node is opened, and this method cannot make good use of space. Then I checked on the Internet that another person's method is to make the array of the Seconddimensional length 62 into an adjacent table, which can save multiple initialization times, this method is called the diagram of the Left brother and right child. It is worth learning. The detailed implementation process depends on the code.
Code:
1 #include <cstdio> 2 #include <cstring> 3 #define MAXN 4001010 4 #define ll long long 5 using namespace std; 6 7 int head[MAXN]; 8 int next[MAXN]; 9 int tot[MAXN];10 int ed[MAXN];11 char ch[MAXN];12 int sz;13 ll sum;14 void init(){15 sz=1; head[0]=next[0]=tot[0]=0; sum=0;16 }17 18 void insert(char *s){19 int u,v,n=strlen(s);20 u=0;21 for(int i=0;i<n;i++){22 bool f=0;23 for(v=head[u];v!=0;v=next[v]){24 if(ch[v]==s[i]){25 f=1; break;26 }27 }28 if(!f){29 v=sz++;30 tot[v]=0;31 ed[v]=0;32 ch[v]=s[i];33 next[v]=head[u];34 head[u]=v;35 head[v]=0;36 }37 u=v;38 sum+=tot[v]*2;39 tot[v]++;40 }41 sum+=ed[u];42 ed[u]++;43 }44 45 46 int main()47 {48 int n;49 char s[1002];50 //freopen("data.txt","r",stdin);51 for(int z=1;scanf("%d",&n),n;z++){52 init();53 for(int i=0;i<n;i++){54 scanf("%s",s);55 insert(s);56 }57 printf("Case %d: %lld\n",z,sum+n*(n1)/2);58 }59 return 0;60 }
/X 11732 */