Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 21372 | Accepted: 7171 |
Description
The other day, Klyde filled an order for the number 31123314 and was amazed to discover that the inventory of this number is the same as the number---it has three 1s, one 2, three 3s, and one 4! He calls this an example of a "self-inventorying number", and now he wants to find out which numbers are self-inventorying, or lead to a self-inventorying number through iterated application of the inventorying operation described below. You have been hired to help him in his investigations.
Given any non-negative integer n, its inventory is another integer consisting of a concatenation of integers c1 d1 c2 d2 ... ck dk , where each ci and di is an unsigned integer, every ci is positive, the di satisfy 0<=d1<d2<...<dk<=9, and, for each digit d that appears anywhere in n, d equals di for some i and d occurs exactly ci times in the decimal representation of n. For instance, to compute the inventory of 5553141 we set c1 = 2, d1 = 1, c2 = 1, d2 = 3, etc., giving 21131435. The number 1000000000000 has inventory 12011 ("twelve 0s, one 1").
An integer n is called self-inventorying if n equals its inventory. It is called self-inventorying after j steps (j>=1) if j is the smallest number such that the value of the j-th iterative application of the inventory function is self-inventorying. For instance, 21221314 is self-inventorying after 2 steps, since the inventory of 21221314 is 31321314, the inventory of 31321314 is 31123314, and 31123314 is self-inventorying.
Finally, n enters an inventory loop of length k (k>=2) if k is the smallest number such that for some integer j (j>=0), the value of the j-th iterative application of the inventory function is the same as the value of the (j + k)-th iterative application. For instance, 314213241519 enters an inventory loop of length 2, since the inventory of 314213241519 is 412223241519 and the inventory of 412223241519 is 314213241519, the original number (we have j = 0 in this case).
Write a program that will read a sequence of non-negative integers and, for each input value, state whether it is self-inventorying, self-inventorying after j steps, enters an inventory loop of length k, or has none of these properties after 15 iterative applications of the inventory function.
Input
Output
n is self-inventorying
n is self-inventorying after j steps
n enters an inventory loop of length k
n can not be classified after 15 iterations
Sample Input
22
31123314
314213241519
21221314
111222234459
-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
Source
对于任意的数字串n,都可以压缩存储为
c1 d1 c2 d2 .... ck dk 形式的数字串
而存在一些特别的数字串,其压缩前后的样子是一模一样的
定义这种数字串为self-inventorying
当我们把n看成原串,
A为n压缩1次后的数字串,
B为n压缩2次后的数字串(即A压缩1次后的数字串)
....以此类推
K为n压缩k次后的数字串(即K-1压缩k-1次后的数字串)
则可以延伸出数字串n的3种属性:
1、 n压缩1次就马上出现self-inventorying特性,即 n n n n n n n .....
2、 n压缩j次后的数字串J出现self-inventorying特性,即 n A B C....H I J J J J J J J
3、 n压缩j次后的数字串J,每再压缩K次,重新出现数字串J,即n A B... J ..K J ..K J..K J
其中K称为循环间隔,K>=2
现给定一字符串,输出其属性。 属性1优于属性2,属性2优于属性3
解题思路:
int大小是32位有符号整型,表示的范围是-2,147,483,648 到 2,147,483,647。80 digits,显然不能使用int存储串
使用char[]进行存储。
然后……模拟
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 int const maxs = 85; 5 void Compress(char a[],char b[]) 6 { 7 int temp[10]; 8 memset(temp,0,sizeof(temp)); 9 for(int i=0;i<strlen(a);i++) 10 { 11 temp[a[i]-'0']++; 12 } 13 int len = 0; 14 for(int i=0;i<10;i++) 15 { 16 if(temp[i]) 17 { 18 if(temp[i]<10) 19 { 20 b[len++] = temp[i] + '0'; 21 b[len++] = i + '0'; 22 }else{ 23 b[len++] = temp[i]/10 + '0'; 24 b[len++] = temp[i]%10 + '0'; 25 b[len++] = i + '0'; 26 } 27 } 28 } 29 b[len] = '\0';///结束符 30 return; 31 } 32 33 int main() 34 { 35 char s[16][maxs];//s[0]为原串 36 while(cin>>s[0] && s[0][0]!='-') 37 { 38 int i; 39 for(i=0;i<15;i++) 40 {///将s[i]压缩,存储到s[i+1]中 41 Compress(s[i],s[i+1]); 42 ///进行判断 43 bool flag1; 44 if(strcmp(s[i],s[i+1])==0)//相同 45 { 46 if(i==0)//只进行了一次压缩就相等 47 { 48 cout<<s[0]; 49 cout<<" is self-inventorying"<<endl; 50 break; 51 }else{ 52 cout<<s[0]; 53 cout<<" is self-inventorying after "<<i<<" steps"<<endl; 54 break; 55 } 56 } 57 int flag3 = 0;///设立情况3的标记,用于跳出循环 58 for(int k=1;k<=i;k++) 59 { 60 if(strcmp(s[i-k],s[i+1])==0) 61 { 62 cout<<s[0]; 63 cout<<" enters an inventory loop of length "<<k+1<<endl; 64 flag3 = 1; 65 break; 66 } 67 } 68 if(flag3) break; 69 } 70 if(i == 15) 71 { 72 cout<<s[0]; 73 cout<<" can not be classified after 15 iterations"<<endl; 74 } 75 } 76 return 0; 77 }