[Problem description] Set R (u) to the relational mode on the property set U, and X and Y to the subset of U. If any possible relationship between R (u) is R, the attribute values of two tuples on X cannot be the same in R, but the attribute values on y cannot be the same, it is called "x function determines y" or "y function depends on X" and is recorded as X → y. X is called the decision Attribute Set (determinant) of this function dependency ). Explanation: if there is a function dependent XY, when we know X, we know y, that is, X can launch y. In addition, it can be proved simply that if XY, YZ, you can get xz. If the value of an attribute group in a link uniquely represents a tuple, the attribute group is called a supercode. If the value of an attribute group in a link can uniquely represent a tuple, but its real subset cannot, the attribute group is called a candidate code. Explanation: There is an attribute set (A, B, C) and function dependency AB and BC. Obviously, when a is known, we can get all the attributes ABC of the entire set through function dependency, so we call a A supercode. When any subset of the supercode is not supercode, we call it a candidate code. For example, AB is a supercode, but not a candidate code. Because a is a subset of AB, it is also a supercode. The property set and function dependency set R (u, f) are provided. Locate the candidate code for this R. [Input file] the first act of the input file is an integer n (n ≤ 10) and M (1 ≤ m ≤ 1000 ), the number of attributes in the attribute set and the number of dependencies in the function dependency set. Here, the first n letters in the upper-case letters are considered attributes by default. Each row in the next M line has a string to indicate a function dependency, such as abde. (The contained symbols in the middle are composed of minus signs and greater than signs. In addition, it should be noted that D and E can be introduced only when we get both A and B ). [Output file] the first line of the output file is to output the number of candidate codes you have found K. The next K rows output a candidate code for each row. Candidate codes are listed alphabetically, and all candidate codes are output alphabetically. If no candidate code is found, "No candidate key" is output (excluding quotation marks ). [Example input] 5 5ab-> Cac-> bad-> EBC-> de-> A [Example input] 4abacbece [data size] for 30% of the test data, only binary connections are allowed (that is, there are more than one attribute on the left or right of the function dependency ). For 40% of the test data, n ≤ 5. For 70% of the test data, m ≤ 100. For 100% of the test data, 1 ≤ n ≤ 10, 1 ≤ m ≤ 1000.
Enumerate each combination (all lengths) to see if it can publish the complete set. If it can, mark all the sets including it as false.
Finally, collect statistics on all possible sets, sort the order by lexicographically, and then output them.
Accode:
# Include <cstdio> # include <algorithm> char STR [40], s [1 <10] [20]; bool tag [1 <10]; int A [1 <10], B [1 <10], n, m; inline bool check (INT X) {int TMP = X; while (TMP <(1 <n)-1) {bool flag = 0; For (INT I = 0; I <m; ++ I) if (A [I] | TMP) = TMP) & (B [I] | TMP)> TMP) {TMP | = B [I]; flag = 1;} If (! Flag) break;} If (TMP = (1 <n)-1) for (INT I = 1; I <1 <n; ++ I) tag [x | I] = 0; // If feasible, mark all the sets containing it as false. Return TMP = (1 <n)-1;} int CMP (const void * a, const void * B) {return strcmp (char *), (char *) B);} int main () {freopen ("dependence. in "," r ", stdin); freopen (" dependence. out "," W ", stdout); scanf (" % d \ n ", & N, & M); For (INT I = 0; I <m; ++ I) {gets (STR); Int J; For (j = 0; STR [J]! = '-'; ++ J) B [I] = A [I] | = 1 <(STR [J]-'A '); for (++ + J; STR [J]; ++ J) B [I] | = 1 <(STR [J]-'A ');} for (INT I = 1; I <1 <n; ++ I) Tag [I] = 1; for (INT I = 1; I <1 <N; ++ I) if (TAG [I]) Tag [I] & = check (I); int CNT = 0; For (INT I = 1; I <1 <n; ++ I) CNT + = tag [I]; If (! CNT) {printf ("no candidate key \ n"); Return 0;} printf ("% d \ n", CNT); CNT = 0; for (INT I = 1; I <1 <n; ++ I) if (TAG [I]) {int x = 0; For (int tmp = I, j = 0; TMP >>= 1, ++ J) if (TMP & 1) sprintf (s [CNT] + (x ++), "% C ", J + 'A'); ++ CNT;} // locate the result and output it to the string for sorting in Lexicographic Order. Qsort (S, CNT, sizeof (char) * 20, CMP); For (INT I = 0; I <CNT; ++ I) printf ("% s \ n ", s [I]); Return 0 ;}