Reprinted please indicate the source: Thank you http://blog.csdn.net/lyy289065406/article/details/6673675
General question:
The meaning of the question is hard to understand. For any number string N, it can be compressed and stored
C1 D1 C2 D2... ck DK digital string
However, there are some special numeric strings which look exactly the same before and after compression.
Define This numeric string as self-Inventorying
When we look at N as the original string,
A is the number string after N is compressed once,
B is the number string after n compression 2 times (that is, the number string after a compression 1 times)
... And so on
K is the number string after n compression K (that is, the number string after K-1 compression k-1)
The following attributes can be extended:
1. The self-Inventorying feature immediately appears when n is compressed once, that is, n .....
2. The self-Inventorying feature of the numeric string J after n compression is displayed, that is, n a B c... h I j
3. After n compression, the number string J is re-displayed, that is, n a B... J .. k J .. k J .. k J
Where k is called the cyclic interval, k> = 2
A string is given and its attributes are output. Property 1 is better than property 2, and property 2 is better than property 3.
Solution:
String processing, purely simulated questions
When compressing N, note that the CK may be 1 or 2 bits and must be determined.
Set R (n) to a compressed numeric string describing integer n
1. When R (n) = R (n), n is self-Inventorying
2. for integer N:
Make n = N
For j = 1 to 15
{Make TJ = R (N)
If R (TJ) = R (TJ), then n is self-Inventorying after J steps and break
Otherwise, n = TJ
}
3. for integer N:
Make n = N, record num [0] = N
For j = 1 to 15
{Make TJ = R (N), record num [J] = TJ
For I = 0 to J-2 (ensure K> = 2)
{If TJ = num [I], then n enters an inventory loop of length k (k = J-I)
Break
}
}
4. When and only when n's three attributes do not exist, n can not be classified after 15 iterations
Source correction
East central North America 1998
Http://plg1.cs.uwaterloo.ca /~ Acm00/regional98/real/
// Memory time // 232 K 32 Ms # include <iostream> # include <string> using namespace STD;/* compress the number string N, store it in T */void R (char * n, char * t) {int I, j; int time [10] = {0 }; // record the number of occurrences of each number in N for (I = 0; n [I]; I ++) time [n [I]-'0'] ++; for (I = 0, j = 0; I <10; I ++) {If (time [I]) {If (time [I] <10) // Number of I occurrences <10, that is, 1 digit {T [J ++] = time [I] + '0 '; T [J ++] = I + '0';} else // number of times a digital I appears> = 10, that is, it occupies 2 places {T [J ++] = time [I]/10 + '0 '; T [J ++] = time [I] % 10 + '0'; t [J ++] = I + '0 ';}}} T [J] = '\ 0'; return;} int Main (int I, Int J) {char N [16] [85]; // n [0] is the original string, N [1 ~ 15] While (CIN> N [0] & N [0] [0]! = '-') {Bool flag1 = false; // attribute 1, n is self-inventoryingint flag2 = 0; // attribute 2, n is self-Inventorying after J steps, by the way, jint flag3 = 0; // property 3, n is enters an inventory loop of length k, and KFOR (I = 1; I <= 15; I ++) is recorded) R (N [I-1], n [I]); If (! Strcmp (N [0], n [1]) // attribute 1. If n is compressed once, its own flag1 = true; If (! Flag1) {for (j = 1; j <15; J ++) if (! Strcmp (N [J], n [J + 1]) // attribute 2, n the number string after J compression N [J] has attribute 1 {flag2 = J; break;} If (! Flag2) {for (j = 1; j <= 15; J ++) // attribute 3, enumerate each compressed numeric string, note loop interval> = 2 {for (I = 0; I <= J-2; I ++) {If (! Strcmp (N [J], n [I]) {flag3 = J-I; break ;}} if (flag3) Break ;}} if (flag1) cout <n [0] <"is self-Inventorying" <Endl; else if (flag2) cout <n [0] <"is self-Inventorying after" <flag2 <"Steps" <Endl; else if (flag3) cout <n [0] <"enters an inventory loop of length" <flag3 <Endl; elsecout <n [0] <"can not be classified after 15 iterations" <Endl;} return 0 ;}
Sample Input
22
31123314
314213241519
21221314
111222234459
123456789
654641656
2101400052100005496
10000000002000000000
333
1
99999999999999999999999999999999999999999999999999999999999999999999999999999999
0000
0001
0111
1111
123456789
456137892
123213241561
543265544536464364
5412314454766464364
543267685643564364
5423434560121016464364
-1
Sample output
22 is self-Inventorying
31123314 is self-Inventorying
314213241519 enters an inventory loop of length 2
21221314 is self-Inventorying after 2 steps
111222234459 enters an inventory loop of length 2
123456789 is self-Inventorying after 5 steps
654641656 enters an inventory loop of length 2
2101400052100005496 enters an inventory loop of length 2
10000000002000000000 enters an inventory loop of length 3
333 is self-Inventorying after 11 steps
1 is self-Inventorying after 12 Steps
99999999999999999999999999999999999999999999999999999999999999999999999999999999 can not be classified after 15 iterations
0000 enters an inventory loop of length 2
0001 is self-Inventorying after 8 Steps
0111 is self-Inventorying after 8 Steps
1111 is self-Inventorying after 8 Steps
123456789 is self-Inventorying after 5 steps
456137892 is self-Inventorying after 5 steps
123213241561 enters an inventory loop of length 2
543265544536464364 enters an inventory loop of length 2
5412314454766464364 is self-Inventorying after 3 steps
543267685643564364 enters an inventory loop of length 2
5423434560121016464364 is self-Inventorying after 3 steps