There are a variety of solutions to this question. Here we use trie to solve it.
You can also use Hash Tables, map, and other solutions. Because the input is a specific 7-digit number, all solutions can be implemented.
The speed of trie is not fast here, mainly because I directly traverse the output and the speed of traversing the entire trie is still relatively slow.
Ideas:
1. Use the insert function to create a trie, which mainly adds information about a leaf node and records the number of duplicate strings.
2 traversal is to determine whether output is required based on the leaf node information.
# Include <stdio. h >#include <iostream >#include <string> using namespace STD; const int arr_size = 10; struct node {int N; node ** Alpha; node (): N (0) {alpha = new node * [arr_size]; for (INT I = 0; I <arr_size; I ++) Alpha [I] = NULL ;}~ Node () {for (INT I = 0; I <arr_size; I ++) {If (alpha [I]) delete Alpha [I]; alpha [I] = NULL;} Delete Alpha; alpha = NULL; n = 0 ;}}; node * trie; void inserttrie (string & S) {node * pcrawl = trie; for (unsigned I = 0; I <S. size (); I ++) {int id = s [I]-'0'; If (pcrawl-> alpha [ID] = NULL) {pcrawl-> alpha [ID] = new node;} pcrawl = pcrawl-> alpha [ID];} pcrawl-> N ++; // increase the number of strings} inline char chartonum (char a) {Switch (a) {Case 'A': Case 'B': Case 'C': Return '2'; Case 'D': Case 'E': Case 'F': Return '3 '; case 'G': Case 'H': Case 'I': Return '4'; Case 'J': Case 'K': Case 'l': Return '5 '; case 'M': Case 'N': Case 'O': Return '6'; case 'p': Case 'r': Case 's': Return '7 '; case 'T': Case 'U': Case 'V': Return '8'; Case 'W': Case 'X': Case 'y ': return '9';} return char ('9' + 1); // There is no mapping for Q or Z. uppercase letters (excluding Q Nd Z)} Char lookuptable [26]; void initlookuptable () {for (INT I = 0; I <26; I ++) {lookuptable [I] = chartonum (char (I + 'A') ;}} void strtonum (string & S) {Int J =-1; for (unsigned I = 0; I <S. size (); I ++) {If (s [I]! = '-') {S [++ J] = s [I]; // error: s [J ++] = s [I]; J has changed, the following uses Jif ('A' <= s [J] & S [J] <= 'Z ') {s [J] = lookuptable [s [J]-'a'] ;}}} S. resize (J + 1);} bool flag; void printrepeat (node * r, string & Path) {If (R-> N> = 1) {If (R-> N = 1) return; flag = true; unsigned I = 0; For (; I <3; I ++) putchar (path [I]); putchar ('-'); For (; I <7; I ++) putchar (path [I]); putchar (''); printf ("% d \ n", R-> N); Return ;}for (INT I = 0; I <arr_s Ize; I ++) // in ascending lexicographical order {path + = char (I + '0'); If (R-> alpha [I]) printrepeat (R-> alpha [I], PATH); Path. erase (path. size ()-1) ;}} int main () {initlookuptable (); int N; scanf ("% d", & N); getchar (); string S; char CHS [100]; trie = new node; while (n --) {gets (CHS); s = CHS; strtonum (s); inserttrie (s );} flag = false; string path; printrepeat (trie, PATH); If (! Flag) puts ("no duplicates."); Delete trie; return 0 ;}