POJ 1016 Numbers That Count(字符串处理)

Numbers That Count
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15752 Accepted: 5160

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



题目要求将数n按一定规则进行转换,比如:1231560,这个数字中有1个0、2个1、1个2、1个3、1个5、1个6,则,其转换后的下一个数字为102112131516,接下去再对这个数字进行转换。

在15次转换之内,可能会发生以下四种情况:m是输入值,j是一个正整数,k是一个大于1的正整数

(1)数字n经过转换一次后,仍为数字n,如:22

这种情况下输出:n is self-inventorying

(2)数字n经过j次转换之后的数字m,的下一次转换结果仍为m,如:21221314

这种情况下输出:n is self-inventorying after j steps 

(3)数字n经过i次转换后的值与其第j次转换相同,进入了一个k = i - j的循环,如:314213241519

这种情况下输出:n enters an inventory loop of length k 

(4)数字n在15次转换之内均没有和之前的情况相同,如:99999999999999999999999...999999999(80个9)

这种情况下输出:n can not be classified after 15 iterations


这道题的算法并不难,思路也比较简单,我因为控制循环的次数出错,让字符串只转换了14次而不是15次,所以WA了3次,解题方法按照上面的解释应该就可以解出来。贴代码:

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>

using namespace std;
map<string, int> StrInt;
char next[200];
char turn[200];
char str[200];

string change(string str){
    int num[10];
    int i, j, k;
    string result;

    memset(num, 0, sizeof(num));
    for (i = 0; i < str.length(); i++){
        num[str[i] - '0']++;
    }
    for (i = j = 0; i < 10; i++){
        if (num[i] != 0){
            memset(turn, 0, sizeof(turn));
            k = 0;
            do {
                turn[k++] = (num[i] % 10);
                num[i] /= 10;
            }while(num[i] != 0);
            for (k = k - 1; k >= 0; k--){
                next[j++] = '0' + turn[k];
            }
            next[j++] = '0' + i;
        }
    }
    next[j] = '\0';
    result = next;
    return result;
}


int main(void){
    int n;
    string atStr;
    int i, j, end;
    int flag = 0;

    while (scanf("%s", str), strcmp(str, "-1") != 0){
        StrInt.clear();
        atStr = str;
        flag = 0;
        for (i = 0; i < 16; i++){
            if (StrInt.count(atStr) == 0){
                StrInt[atStr] = i;
            }
            else{
                j = StrInt[atStr];
                if (i == 1 && j == 0){
                    flag = 1;
                    printf("%s is self-inventorying\n", str);
                }
                else if (j == i - 1){
                    flag = 2;
                    end = j;
                    printf("%s is self-inventorying after %d steps\n", str, end);
                }
                else{
                    flag = 3;
                    end = i - j;
                    printf("%s enters an inventory loop of length %d\n", str, end);
                }
                break;
            }
            atStr = change(atStr);
        }
        if (flag == 0){
            printf("%s can not be classified after 15 iterations\n", str);
        }
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值