How many
Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 1028 accepted submission (s): 403
Problem descriptiongive you n (n <10000) necklaces, the length of neck?will not large than 100, tell me
How many kinds of necklaces total have. (If two necklaces can equal by rotating, we say the two necklaces are some ).
For example 0110 express a neck.pdf, you can rotate it. 0110-> 1100-> 1001-> 0011-> 0110.
Inputthe input contains multiple test cases.
Each test case include: First one integers n. (2 <= n <= 10000)
Next n lines follow. Each line has a equal length character string. (string only include '0', '1 ').
Outputfor each test case output a integer, how many different necklaces.
Sample Input
4011011001001001141010010110000001
Sample output
12
The meaning of the question is very understandable. If you have n strings, all of them can be cyclically shifted. Many of them are homogeneous. How many differences can be found, after transformation, the values can be equal. It's a bit like chemical heterogeneity...
Solution: the initial idea is to copy each string twice and then use its own minimum representation to copy the string. The minimum representation of % is not used. However, it takes a lot of time in strcpy and strcat, later, the smallest representation of % was directly used, but it had been entangled For A Long Time During sort sorting. Finally, we had no choice but to replace the starting char two-dimensional array with a struct, which is 26 Ms seconds later.
After reading it online, set can also be fixed for 26 Ms. For details, see the code.
Question address: How many
AC code 1: Direct sorting method
# Include <iostream> # include <cstring> # include <string> # include <algorithm> # include <cstdio> using namespace STD; char a [10005] [105]; char P [105]; // The sort cannot directly rank two-dimensional Char? Int N; struct node {char STR [105] ;}; Node B [10005]; // each string is the smallest int CMP (node No1, node NO2) {If (strcmp (no1.str, no2.str) <0) return 1; return 0;} void getmin () {int I, J, K, L; For (L = 0; L <n; l ++) {int Len = strlen (A [l]); I = 0, j = 1, K = 0; while (I <Len & J <Len & K <Len) {int T = A [l] [(I + k) % Len]-A [l] [(j + k) % Len]; If (t = 0) K ++; else if (T <0) {J = J + k + 1; k = 0;} else {I = I + k + 1; k = 0;} if (I = J) j ++;} // I is the minimum position, and then the minimum value is expressed to B. Int is allowed. Pos =-1; for (k = I; k <Len; k ++) B [l]. STR [++ POS] = A [l] [k]; for (k = 0; k <I; k ++) B [l]. STR [++ POS] = A [l] [k]; B [l]. STR [Len] = '\ 0'; // cout <B [l]. STR <Endl ;}int main () {int I; while (~ Scanf ("% d", & N) {int CNT = 1; for (I = 0; I <n; I ++) scanf ("% s ", A [I]); getmin (); // obtain the minimum sort (B, B + N, CMP) for each string; for (I = 1; I <N; I ++) if (strcmp (B [I]. STR, B [I-1]. str )! = 0) CNT ++; printf ("% d \ n", CNT) ;}return 0 ;}
AC code 2: Minimum expression + setset operations can be referred to: STL set basic operations
# Include <iostream> # include <cstring> # include <string> # include <algorithm> # include <cstdio> # include <set> using namespace STD; char A [10005] [105]; char STR [105]; set <string> MQ; int CNT, N; void getmin () {MQ. clear (); CNT = 0; int I, J, K, L; For (L = 0; L <n; l ++) {int Len = strlen (A [l]); I = 0, j = 1, K = 0; while (I <Len & J <Len & K <Len) {int T = A [l] [(I + k) % Len]-A [l] [(j + k) % Len]; If (t = 0) k ++; else if (T <0) {J = J + k + 1; k = 0;} else {I = I + k + 1; k = 0;} if (I = J) J ++;} // I is the minimum position, then the minimum value is expressed to B, and INT Pos =-1; for (k = I; k <Len; k ++) STR [++ POS] = A [l] [k]; for (k = 0; k <I; k ++) STR [++ POS] = A [l] [k]; STR [Len] = '\ 0'; // The following is set usage. If (! MQ. Count (STR) {CNT ++; MQ. insert (STR) ;}} int main () {int I; while (~ Scanf ("% d", & N) {for (I = 0; I <n; I ++) scanf ("% s", a [I]); getmin (); // obtain the minimum printf ("% d \ n", CNT);} return 0 ;}
If you want to copy a single copy, do not select string ....