POJ 1016 模拟字符串

Numbers That Count

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 20396   Accepted: 6817

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

题意:

水题题意太繁就不写了吧。

代码:

 1 /*
 2 坑爹的题,简单的模拟只是字符串处理不好弄,刚开始想用itoa函数直接把数字加到字符串后面,但在POJ上ce了应该是不支持这个函数
 3 后来发现一个数字竟然可以赋值给一个字符变量,这样就可以把数字一个一个的加到字符数组里(最后要把\0加到字符数组里),C学的不好。
 4 */
 5 #include<iostream>
 6 #include<cstdio>
 7 #include<cstring>
 8 #include<string>
 9 using namespace std;
10 char ch[20][90];
11 int a[10];
12 int main()
13 {
14     while(scanf("%s",&ch[0])!=EOF)
15     {
16         if(!strcmp(ch[0],"-1")) break;
17         bool flag=0;
18         for(int k=1;k<=15;k++){
19         memset(a,0,sizeof(a));
20         int lne=strlen(ch[k-1]);
21         for(int i=0;i<lne;i++)
22         a[ch[k-1][i]-‘0‘]++;
23         char s[90];
24         int t=0;
25         for(int i=0;i<10;i++)
26         {
27             if(a[i]==0)
28             continue;
29             if(a[i]>=10)
30             {
31                 s[t++]=a[i]/10+‘0‘;
32                 s[t++]=a[i]%10+‘0‘;
33             }
34             else s[t++]=a[i]+‘0‘;
35             s[t++]=i+‘0‘;
36         }
37         s[t]=‘\0‘;
38         if(!strcmp(s,ch[k-1]))
39         {
40             if(k==1)
41             printf("%s is self-inventorying\n",ch[0]);
42             else
43             printf("%s is self-inventorying after %d steps\n",ch[0],k-1);
44             flag=1;
45         }
46         else
47         {
48             for(int i=k-2;i>=0;i--)
49             if(!strcmp(s,ch[i]))
50             {
51                 printf("%s enters an inventory loop of length %d\n",ch[0],k-i);
52                 flag=1;
53                 break;
54             }
55         }
56         if(flag) break;
57         strcpy(ch[k],s);
58         }
59         if(!flag)
60         printf("%s can not be classified after 15 iterations\n",ch[0]);
61     }
62     return 0;
63 }
时间: 2024-10-12 09:11:49

POJ 1016 模拟字符串的相关文章

Poj 1016

传送门:http://poj.org/problem?id=1016 数字字符串的压缩,比较 注意指针的使用,注意细节 1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 5 void inventory(char *s,char *d){ 6 int n[10]={0}; 7 for(int i=0;s[i];i++){ 8 n[s[i]-'0']++; 9 } 10 int j=0; 11 for(

POJ 2420 模拟退火法

A Star not a Tree? Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3272   Accepted: 1664 Description Luke wants to upgrade his home computer network from 10mbs to 100mbs. His existing network uses 10base2 (coaxial) cables that allow you

Shuffle&#39;m Up (poj 3087 模拟)

Language: Default Shuffle'm Up Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5968   Accepted: 2802 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by starting

模拟/字符串处理 UVALive 6833 Miscalculatio

题目传送门 1 /* 2 模拟/字符串处理:主要是对*的处理,先把乘的预处理后再用加法,比如说是:1+2*3+4 = 1+..6+4 = 11 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cstring> 7 #include <string> 8 #include <cmath> 9 #include <vector> 10 #include <map

G - Shuffle&#39;m Up POJ 3087 模拟洗牌的过程,算作暴力搜索也不为过

G - Shuffle'm Up Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3087 Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuffling chips is performed by start

洛谷P1449 后缀表达式 栈 模拟 字符串

洛谷P1449 后缀表达式 栈 模拟 字符串 栈模拟一下 碰到 . 如果输入的是数字就把数字放进栈中 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <cstdlib> 5 #include <string> 6 #include <algorithm> 7 #include <iomanip> 8 #include <io

java基础知识回顾之---java String final类普通方法的应用之“模拟字符串Trim方法”

/* * 4,模拟一个trim功能一致的方法.去除字符串两端的空白  * 思路: * 1,定义两个变量. * 一个变量作为从头开始判断字符串空格的角标.不断++. * 一个变量作为从尾开始判断字符串空格的角标.不断--. * 2,判断到不是空格为止,取头尾之间的字符串即可. *  *  使用char charAt(int index);方法根据index索引,取出字符串 *  使用String substring(int beginIndex, int endIndex)//包含begin 不包

POJ 1016 Numbers That Count 模拟题目

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 e

poj 3087 模拟

背景:bfs专题的题,可是直接模拟就好了啊. 思路:管件在于记录第一个s12串,当再次出现第一个s12串时说明进入了循环之中,不能呢达到目标状态. 学习:1.strcmp时要注意,该字符串的有效部分是不是以'\0'结尾的. #include<map> #include<set> #include<stack> #include<queue> #include<vector> #include<cstdio> #include<c