poj 1016 Numbers That Count
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 17922 | Accepted: 5940 |
Description
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
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
题目大意:给一个大数,问我们经过多少次操作以后可以得到一个循环,操作的方法就是统计这个数中由小到大的数字出现的次数,然后写成 (数字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,看来是我想太多了。。。。
#include<stdio.h>
#include<map>
#include<string>
#include<string.h>
#include<iostream>
using namespace std;
string solve(string str)
{
int i;
int hash[10] = {0};
int len = str.length();
for(i = 0; i < len; i++)
{
hash[str[i] - '0'] ++;
}
string new_str;
char ch[30] = {0};
for(i = 0; i < 10; i++)
{
if(hash[i] != 0)
{
sprintf(ch, "%d%d", hash[i], i);
new_str.append(ch); }
}
return new_str;
}
int main()
{
// freopen("in.txt", "r", stdin);
string s;
while(cin >> s)
{
if(s == "-1")
break;
map<string, int> mymap;
mymap[s] = 0;
string new_str = s, prev;
int i;
int flag = 0;
for(i = 1; i < 16; i++)
{
prev = new_str;
new_str = solve(new_str);
if(prev == new_str)
{
flag = 1;
break;
}
if(mymap[new_str] != 0)
{
flag = i - mymap[new_str];
break;
}
else
mymap[new_str] = i;
}
if(i == 1)
cout << s << " is self-inventorying" << endl;
else if(i < 16)
{
if(flag == 1)
cout << s << " is self-inventorying after " << i - 1 << " steps" << endl;
else
cout << s << " enters an inventory loop of length " << flag << endl;
}
else
cout << s << " can not be classified after 15 iterations" << endl;
}
return 0;
}
poj 1016 Numbers That Count的更多相关文章
- POJ 1016 Numbers That Count 不难,但要注意细节
题意是将一串数字转换成另一种形式.比如5553141转换成2个1,1个3,1个4,3个5,即21131435.1000000000000转换成12011.数字的个数是可能超过9个的.n个m,m是从小到 ...
- POJ1016 Numbers That Count
题目来源:http://poj.org/problem?id=1016 题目大意: 对一个非负整数定义一种运算(inventory):数这个数中各个数字出现的次数,然后按顺序记录下来.比如“55531 ...
- POJ 1016 模拟字符串
Numbers That Count Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20396 Accepted: 68 ...
- Numbers That Count POJ - 1016
"Kronecker's Knumbers" is a little company that manufactures plastic digits for use in sig ...
- POJ 1016
http://poj.org/problem?id=1016 一道字符串处理的题目,理解题意后注意细节就好. 题意:每一串数字 都可以写成 a1 b1 a2 b2 ....ai bi 其中ai是指bi ...
- B - Numbers That Count
Description "Kronecker's Knumbers" is a little company that manufactures plastic di ...
- poj Pseudoprime numbers 3641
Pseudoprime numbers Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10903 Accepted: 4 ...
- POJ Round Numbers(数位DP)
题目大意: Round Number: 将一个整数转化为二进制数字后,(不含前导0) 要是0的个数 大于等于1的个数 则是 Round Number 问从L-R之中有多少个Round Number ...
- POJ Pseudoprime numbers( Miller-Rabin素数测试 )
链接:传送门 题意:题目给出费马小定理:Fermat's theorem states that for any prime number p and for any integer a > 1 ...
随机推荐
- android 二维码扫描
了解二维码这个东西还是从微信 中,当时微信推出二维码扫描功能,自己感觉挺新颖的,从一张图片中扫一下竟然能直接加好友,不可思议啊,那时候还不了解二维码,呵呵,然后做项目的时候, 老板说要加上二维码扫描功 ...
- windows 2003 server 远程桌面禁用本地资源,磁盘驱动器,串行口,复制文件
首先进入组策略编辑器(开始-运行-gpedit.msc) 不要让用户在远端桌面和本地直接拷贝文件在远端桌面上进入它的组策略编辑器在 计算机配置->管理模板->Windows组件->终 ...
- Knockout应用开发指南(完整版) 目录索引
http://learn.knockoutjs.com/ 所有示例和代码都在在上面直接运行预览 注意:因为它用了google的cdn加速,所要要用代_理+_翻_墙才能正常加载 使用Knockout有 ...
- jQuery validate在没有校验通过的情况下拒绝提交
下面通过一个简单的例子说明,这个问题,可能是很多人遇到的,验证不通过的时候,依然提交了表单. HTML <form class="survey" id="surve ...
- Spring入门学习(一)
SpringMVC基础平台补充(2016.03.03) 如果想要开发SpringMVC,那么前期依次安装好:JDK(jdk-8u74-windows-x64,安装后配置环境变量JAVA_HOME和CL ...
- HTML 元素
HTML 文档是由 HTML 元素定义的. HTML 元素 HTML 元素指的是从开始标签(start tag)到结束标签(end tag)的所有代码. 开始标签 元素内容 结束标签 <p> ...
- WeX5和BeX5比较
http://wex5.com/cn/wex5和bex5比较/ WeX5和BeX5比较 许多对WeX5和BeX5略有了解得人都知道,WeX5和BeX5是完全共用前端框架技术的.但是WeX5和BeX5是 ...
- c++封装编写线程池
在csapp学习或者其他linux底层编程的过程中,一般都会举一些多线程或多进程的例子,配合底层同步原语.系统调用api来解释怎么创建多线程/多进程. 但是这些例子和实际项目中所用到的多线程/多进程编 ...
- RedisCacheTool参考其中的文件读写功能
package com.jr.market.tool; import java.io.BufferedReader; import java.io.File; import java.io.FileI ...
- eclipse 每次切换工作空间都要重新配置
首先,导出T1中的配置打开T1,选择file --> Export --> 在弹出框中选择General 下的preference --> next --> 在export p ...