Question:
Determine whether two phone numbers can constitute a prefix relationship
Analysis:
The dynamic build tree is indeed time limitted. It can only be a static build tree or directly arrange the Lexicographic Order and then compare the adjacent two
Can constitute a prefix relationship... Let's briefly talk about the dynamic tree structure (it should be correct ..)
The followingProgramBecause time limitted is determined, it should be correct...
# Include <iostream>
# Include <cstdio>
Using namespace STD;
Struct Trie
{// Define the dictionary tree
Bool end;
Bool ID;
Trie * P [10]; // a total of 10 digits
Trie ()
{
End = false; // determines whether a word exists at the root of the node. If yes, true is returned.
Id = false; // determines whether a word has been used and returns to the root number from here.
For (INT I = 0; I <10; I ++) // No number at the beginning, set null
P [I] = NULL;
}
} Root;
Bool make_tree (struct trie * r, char * s)
{// Dynamic Tree Function
For (INT I = 0; s [I]; I ++)
{
If (R-> P [s [I]-'0'] = NULL) // if the number is not used previously, there is no prefix relationship.
R-> P [s [I]-'0'] = new trie ();
Else if (R-> end) // if this location already has a number and the number ends here, false is returned.
Return false;
R-> id = true; // mark that the number has passed
R = r-> P [s [I]-'0'];
If (R-> end) // If a number already exists at this location and the number ends at this location, false is returned.
Return false;
}
If (R-> id = 1 | r-> end) // the existing number ends here or the current number is the prefix of another person.
Return false;
R-> id = true; // mark that the number has passed
R-> end = true; // mark that a number already exists here and ends here
Return true;
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int t, n;
Char s [12];
Cin> T;
While (t --)
{
Struct trie QQ;
Int flag = 0; // indicates whether a prefix is found.
Scanf ("% d", & N );
For (INT I = 0; I <n; I ++)
{
Scanf ("% s", S );
If (! Flag &&! Make_tree (& QQ, S ))
Flag = 1;
}
If (FLAG)
Printf ("NO \ n ");
Else
Printf ("Yes \ n ");
}
Return 0;
}
Method 1:
You can directly use the quick sort function to sort the order and then compare whether the prefix relationship is formed between the first and last two.
# Include <iostream>
# Include <algorithm>
# Include <string>
# Include <cstdio>
Using namespace STD;
# Define x 10005
String s [x];
Int CMP (string S1, string S2) // internal comparison Function
{
Return S1 <S2;
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int t, n;
Scanf ("% d", & T );
While (t --)
{
Scanf ("% d", & N );
For (INT I = 0; I <n; I ++)
Cin> S [I];
Sort (S, S + N, CMP); // sort directly
Bool flag = 0;
Int I, J;
For (I = 0; I <n-1; I ++)
{
Int Len = s [I]. Size ();
For (j = 0; j <Len; j ++)
If (s [I] [J]! = S [I + 1] [J]) // if there is an unequal value, it cannot constitute a prefix relationship.
Break;
If (j = Len) // indicates a prefix relationship.
{
Flag = true;
Break;
}
}
If (FLAG)
Printf ("NO \ n ");
Else
Printf ("Yes \ n ");
}
}
Static dictionary method:
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <cstdlib>
Using namespace STD;
# Define maxn60002
Int sum;
Struct node // equivalent to a static trie tree
{
Int next [11];
} Tree [maxn];
Struct phone // to sort the two-dimensional character array, use the struct to store the entered phone number.
{
Char in [11];
} Phone [maxn];
Int CMP (const void * a, const void * B) //AlgorithmYou must start with a long string before inserting it.
{
Struct phone * c = (struct phone *);
Struct phone * D = (struct phone *) B;
Return strlen (D-> In)-strlen (c-> In );
}
Bool insert (char * Ch) // insert dictionary
{
Int C, num = 0;
Bool flag = false;
For (INT I = 0; ch [I]; I ++)
{
C = CH [I]-'0 ';
If (tree [num]. Next [c] = 0) // if the current position is null, it indicates that this number is not prefixed.
{
Flag = true; // The flag can be set to true.
Tree [num]. Next [c] = ++ sum;
Num = sum; // pointer Movement
}
Else
Num = tree [num]. Next [c];
}
Return flag; // return flag
}
Int main ()
{
Freopen ("sum. In", "r", stdin );
Freopen ("sum. Out", "W", stdout );
Int t, n;
Cin> T;
While (t --)
{
Memset (tree, 0, sizeof (tree ));
Bool flag = true;
Scanf ("% d", & N );
For (INT I = 0; I <n; I ++)
Scanf ("% s", phone [I]. In );
Qsort (phone, N, sizeof (phone [0]), CMP );
Sum = 0;
For (INT I = 0; I <n & flag; I ++) // If I is less than N and there is no prefix relationship, continue to insert
If (! Insert (phone [I]. In) // if there is a prefix relationship
Flag = false;
If (FLAG)
Printf ("Yes \ n ");
Else
Printf ("NO \ n ");
}
Return 0;
}
Loading editor...