poj1016

来源:互联网 发布:js设置背景颜色 编辑:程序博客网 时间:2024/05/21 19:38
Numbers That Count
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 20986 Accepted: 7016

Description

"Kronecker's Knumbers" is a little company that manufactures plastic digits for use in signs (theater marquees, gas station price displays, and so on). The owner and sole employee, Klyde Kronecker, keeps track of how many digits of each type he has used by maintaining an inventory book. For instance, if he has just made a sign containing the telephone number "5553141", he'll write down the number "5553141" in one column of his book, and in the next column he'll list how many of each digit he used: two 1s, one 3, one 4, and three 5s. (Digits that don't get used don't appear in the inventory.) He writes the inventory in condensed form, like this: "21131435". 

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

A sequence of non-negative integers, each having at most 80 digits, followed by the terminating value -1. There are no extra leading zeros.

Output

For each non-negative input value n, output the appropriate choice from among the following messages (where n is the input value, j is a positive integer, and k is a positive integer greater than 1): 
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

East Central North America 1998

题目大意:现在有一个规则,给定一个数,统计其中数字(0~9)出现的次数,然后从小到大重新组成一个数。如112233,其中1出现2次,2出现2次,3出现2次,那么新的数为212223.现在有一个位数至多为80位的数,通过变换后根据题目中给定的输出的规定进行输出。
1.n is self-inventorying 
2.n is self-inventorying after j steps 
3.n enters an inventory loop of length k 
4.n can not be classified after 15 iterations
如果一个数变换一次过后仍为本身按1输出,如果不为本身,但是经过n(n<=15)次变换后为原来的数,则按2输出,如果超过15次仍未出现,但是在这15次变换中有数是相同的,求出这个相同两个数的最小长度,然后按3输出,最后若15次变换内没有出现上述每一种条件,则按4输出。需要注意的是先要执行上面的情况后才能考虑下面的情况。

题目分析:首先由一个函数来执行这种变换规则,最后分别在不同的函数中获得这个规则变换后的字符串,然后进行判断,通过判断结果进行输出。

代码:
#include <iostream>#include <string>#include <memory.h>#include <map>#include <stdio.h>using namespace std;int a[10];string str;int flag,length,step;map<string,int>station;string fun1(string str){memset(a,0,sizeof(a));string str1;for(unsigned int i=0;i<str.length();i++){int m=str[i]-'0';a[m]++;}for(int i=0;i<10;i++){if(a[i]){string str2;while(a[i]){str2+=char(a[i]%10+'0');a[i]/=10;}int len=str2.length()-1;for(int j=len;j>=0;j--){str1+=str2[j];}str1=str1+char(i+'0');}}return str1;}void fun2(string str2,int steps){string str1=fun1(str2);if(str1==str2||steps>=15){step=steps;return;}fun2(str1,steps+1);}void fun3(string str3,int len){    if(len>=15){length=len;return;}if(len==0){station.insert(pair<string,int>(str3,len));}string str1=fun1(str3);if(station.find(str1)==station.end()){station.insert(pair<string,int>(str1,len+1));}else{length=len-station[str1];return;}fun3(str1,len+1);}void out(string str1){if(str==str1){cout<<str<<" is self-inventorying"<<endl;}else{flag=0;fun2(str,0);if(step<15){cout<<str<<" is self-inventorying after "<<step<<" steps"<<endl;}else{flag=0;fun3(str,0);if(length+1<15){cout<<str<<" enters an inventory loop of length "<<length+1<<endl;}else{cout<<str<<" can not be classified after 15 iterations"<<endl;}}}}int main(){    //freopen("in.txt","r",stdin);while(cin>>str){while(!station.empty()){station.clear();}if(str[0]=='-'&&str[1]=='1'){break;}string str1=fun1(str);out(str1);}return 0;}



原创粉丝点击