04: virus, 04 Virus
04: Virus
- View
- Submit
- Statistics
- Question
-
Total time limit:
-
1000 ms
-
Memory limit:
-
65535kB
-
Description
-
One day, John suddenly found his computer infected with a virus! Fortunately, John finds the virus weak, but replaces all the letters in the document with other letters, without changing the order or adding or deleting letters.
How can I restore the original document now! John is very clever. He generates a dictionary composed of several words on other machines without virus infection. The words in the dictionary are listed in alphabetical order, he copied the file to his machine and deliberately infected it with viruses. He wanted to use the original order of the dictionary file to find the rule of replacing letters with viruses, it is used to restore other documents.
Now, your task is to tell you that the dictionary is infected with a virus and you want to restore the dictionary string.
-
Input
-
Virus. in
The first integer K (≤ 50000) indicates the number of words in the dictionary.
The following K rows are virus-infected dictionaries with one word per row.
The last line is a string of letters that need to be restored.
All letters are in lowercase.
-
Output
-
Virus. out
Only one row is output, which is a string of letters after restoration. Of course, the dictionary may be incomplete or even incorrect. In this case, output 0.
-
Sample Input
-
6cebdbaccacecddcaababaccedab
-
Sample output
-
Abcde
Let's take a look at the code,
Note Special Judgment
1 # include <iostream> 2 # include <cstdio> 3 # include <cstring> 4 # include <stack> 5 # include <cstdlib> 6 # include <cmath> 7 const int MAXN = 50000; 8 using namespace std; 9 string a [MAXN]; 10 int maxnlength; 11 struct node 12 {13 int u; 14 int v; 15 int w; 16 int next; 17} edge [MAXN]; 18 int rudu [MAXN]; 19 int head [MAXN]; 20 int num = 1; 21 int ans [10001]; // Save the result of topological sorting 22 int now = 1; 23 int n; 24 int maxnchar = 0; 25 int map [101] [101]; 26 void topsort () 27 {28 stack <int> s; 29 for (int I = 1; I <= maxnchar; I ++) 30 {31 if (rudu [I] = 0) 32 {33 s. push (I); 34 ans [now] = I; 35 now ++; 36} 37} 38 int flag = 0; 39 while (s. size () = 1) 40 {41 if (s. size ()> 1) 42 {43 flag = 1; 44 break; 45} 46 int p = s. top (); 47 s. pop (); 48 for (int I = head [p]; I! =-1; I = edge [I]. next) 49 {50 rudu [edge [I]. v] --; 51 if (rudu [edge [I]. v] = 0) 52 {53 s. push (edge [I]. v); 54 ans [now] = edge [I]. v; 55 now ++; 56} 57} 58} 59 if (flag = 1) 60 {61 printf ("0 \ n"); 62 exit (0 ); 63} 64} 65 int main () 66 {67 68 scanf ("% d", & n); 69 for (int I = 1; I <= n; I ++) head [I] =-1; 70 for (int I = 1; I <= n; I ++) 71 {72 cin> a [I]; 73 if (a [I]. length ()> maxnlength) 74 maxnlength = a [I]. length (); 75 for (int J = 1; j <= a [I]. length (); j ++) 76 {77 if (a [I] [j]-96> maxnchar) 78 maxnchar = a [I] [j]-96; 79} 80} 81 int flag2 = 0; 82 for (int I = 2; I <= n; I ++) 83 {84 int j = I-1; 85 for (int k = 0; k <= min (a [I]. length ()-1, a [j]. length ()-1); k ++) 86 {87 if (a [j] [k]! = A [I] [k]) 88 {89 if (map [a [j] [k]-96] [a [I] [k]-96] = 1 | map [a [I] [k]-96] [a [j] [k]-96] = 1) 90 {91 printf ("0 \ n"); 92 return 0; 93} 94 edge [num]. u = a [j] [k]-96; 95 edge [num]. v = a [I] [k]-96; 96 edge [num]. next = head [edge [num]. u]; 97 head [edge [num]. u] = num ++; 98 rudu [a [I] [k]-96] ++; 99 flag2 = 1; 100 map [a [j] [k]-96] [a [I] [k]-96] = 1; 101 break; 102} 103} 104 // if (flag2 = 1) break; 105} 106 topsort (); 107 char sr [101]; 108 char huiche [1]; 109 gets (huiche); 110 gets (sr); 111 int l = strlen (sr); 112 // int srl = sr. length (); 113 for (int I = 0; I <= l; I ++) 114 {115 if (sr [I]-96> maxnchar) 116 {117 printf ("0 \ n"); 118 return 0; 119} 120} 121 for (int I = 0; I <= l; I ++) 122 {123 for (int j = 1; j <= now-1; j ++) 124 {125 if (ans [j] = sr [I]-96) 126 {127 printf ("% c", char (j + 96); 128} 129} 130 return 0; 131}