点击打开链接

Numbers That Count
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 17922   Accepted: 5940

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

  1. 22
  2. 31123314
  3. 314213241519
  4. 21221314
  5. 111222234459
  6. -1

Sample Output

  1. 22 is self-inventorying
  2. 31123314 is self-inventorying
  3. 314213241519 enters an inventory loop of length 2
  4. 21221314 is self-inventorying after 2 steps
  5. 111222234459 enters an inventory loop of length 2

题目大意:给一个大数,问我们经过多少次操作以后可以得到一个循环,操作的方法就是统计这个数中由小到大的数字出现的次数,然后写成  (数字1出现次数)1(数字2出现次数)2这样的形式,没有这个数字就不写。

问经过多少次操作可以出现循环,。循环的结果有4种:

1、本身就是一个循环,就是说数字a操作一个还是数字a : a->a->a->a

2、经过n步以后变成条件1的情况:a->b->c->c->c->c->c

3、经过n步以后构成了一个环:a->b->c->a->b->c

4、经过15次操作依然没出现以上3种情况

题目就是一个模拟题,不过判重我用了一个map把字符串映射一个数字,但速度很慢 200+ms,后来看了别人的思路,就是存在一个字符串数组里顺序比较就行了,效率很高32MS,看来是我想太多了。。。。

  1. #include<stdio.h>
  2. #include<map>
  3. #include<string>
  4. #include<string.h>
  5. #include<iostream>
  6. using namespace std;
  7. string solve(string str)
  8. {
  9. int i;
  10. int hash[10] = {0};
  11. int len = str.length();
  12. for(i = 0; i < len; i++)
  13. {
  14. hash[str[i] - '0'] ++;
  15. }
  16. string new_str;
  17. char ch[30] = {0};
  18. for(i = 0; i < 10; i++)
  19. {
  20. if(hash[i] != 0)
  21. {
  22. sprintf(ch, "%d%d", hash[i], i);
  23. new_str.append(ch);
  24.  
  25. }
  26. }
  27. return new_str;
  28. }
  29. int main()
  30. {
  31. // freopen("in.txt", "r", stdin);
  32. string s;
  33. while(cin >> s)
  34. {
  35. if(s == "-1")
  36. break;
  37. map<string, int> mymap;
  38. mymap[s] = 0;
  39. string new_str = s, prev;
  40. int i;
  41. int flag = 0;
  42. for(i = 1; i < 16; i++)
  43. {
  44. prev = new_str;
  45. new_str = solve(new_str);
  46. if(prev == new_str)
  47. {
  48. flag = 1;
  49. break;
  50. }
  51. if(mymap[new_str] != 0)
  52. {
  53. flag = i - mymap[new_str];
  54. break;
  55. }
  56. else
  57. mymap[new_str] = i;
  58. }
  59. if(i == 1)
  60. cout << s << " is self-inventorying" << endl;
  61. else if(i < 16)
  62. {
  63. if(flag == 1)
  64. cout << s << " is self-inventorying after " << i - 1 << " steps" << endl;
  65. else
  66. cout << s << " enters an inventory loop of length " << flag << endl;
  67. }
  68. else
  69. cout << s << " can not be classified after 15 iterations" << endl;
  70. }
  71. return 0;
  72. }

poj 1016 Numbers That Count的更多相关文章

  1. POJ 1016 Numbers That Count 不难,但要注意细节

    题意是将一串数字转换成另一种形式.比如5553141转换成2个1,1个3,1个4,3个5,即21131435.1000000000000转换成12011.数字的个数是可能超过9个的.n个m,m是从小到 ...

  2. POJ1016 Numbers That Count

    题目来源:http://poj.org/problem?id=1016 题目大意: 对一个非负整数定义一种运算(inventory):数这个数中各个数字出现的次数,然后按顺序记录下来.比如“55531 ...

  3. POJ 1016 模拟字符串

    Numbers That Count Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20396   Accepted: 68 ...

  4. Numbers That Count POJ - 1016

    "Kronecker's Knumbers" is a little company that manufactures plastic digits for use in sig ...

  5. POJ 1016

    http://poj.org/problem?id=1016 一道字符串处理的题目,理解题意后注意细节就好. 题意:每一串数字 都可以写成 a1 b1 a2 b2 ....ai bi 其中ai是指bi ...

  6. B - Numbers That Count

    Description        "Kronecker's Knumbers" is a little company that manufactures plastic di ...

  7. poj Pseudoprime numbers 3641

    Pseudoprime numbers Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10903   Accepted: 4 ...

  8. POJ Round Numbers(数位DP)

    题目大意: Round Number:  将一个整数转化为二进制数字后,(不含前导0) 要是0的个数 大于等于1的个数 则是 Round Number 问从L-R之中有多少个Round Number ...

  9. POJ Pseudoprime numbers( Miller-Rabin素数测试 )

    链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1 ...

随机推荐

  1. Linux 实现自动安装服务组件以及优化内核参数 (转)

    安装好Linux裸机后(安装请参考:http://blog.itpub.net/26230597/viewspace-1380155/),还需要在其上安装一些基础组件,一般是手动一个个安装,比较繁复也 ...

  2. Redis Cluster 在PHP上的实践

    版本:redis3.0.3 redis——slot: 'master' => [ '192.168.1.55:7000'=>[0,5460], '192.168.1.55:7001'=&g ...

  3. ason 和 Java 对象转化示例

    1.工程 2.代码: JsonUtil.java package com.my.json; import java.util.ArrayList; import java.util.List; imp ...

  4. smarty变量调节器案例

    要求: 如下图,有内容的每一行,当鼠标放上去显示灰色区域,当鼠标离开灰色区域消失

  5. 【Andorid开发框架学习】之Mina开发之服务器开发

    下午那篇博客我们讲到了Mina的客户端的开发,如果还有没看过的同学可以看一下,我是传送门.现在,我们来学习一下,Mina的服务器的开发. 一.首先看一下,我的服务器的代码图片:  服务器代码我是在My ...

  6. AppCan做的图片上传代码

    存在AppCan里的网页 index.html <!DOCTYPE html> <html class="um landscape min-width-240px min- ...

  7. read,for,case,while,if简单例子

    Read多用于从某文件中取出每行进行处理 $ cat read.sh #!/bin/bash echo "using read" cat name.txt | while read ...

  8. jquery .filter()过滤器

    述: 筛选元素集合中匹配表达式 或 通过传递函数测试的 那些元素集合. .filter( selector ) selector 类型: Selector                 一个用于匹配 ...

  9. JavaScript 设置、读取Cookie

    1.设置Cookie //设置cookie function setCookie(cookieName, cookieValue, cookieExpires, cookiePath) { cooki ...

  10. 发布一个.net mvc站点遇到的问题及解决

    1.先通过vs2012发布.net mvc项目,遇到问题是一路默认下来,提示发布已成功,但对应文件夹里没有任何文件 解决: 第一步,新建了一个文件夹 第二步,在[配置文件]步骤,新建配置文件 第三步, ...