POJ 1016 Numbers that Count simulation topic

Source: Internet
Author: User

Description

"Kronecker's Knumbers" is a little company this manufactures plastic digits for use with signs (theater marquees, Gas statio N price displays, and so on). The owner and sole employee, Klyde Kronecker, keeps track of what many digits of each type he had used by maintaining a in Ventory Book. For instance, if he had 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's ll list how many of each digit he used:two 1s, one 3, one 4, and THR EE 5s. (Digits that Don's get used don ' t appear in the inventory.) He writes the inventory in condensed form, like this: "21131435".

The other day, Klyde filled a order for the number 31123314 and is 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 the example of a "self-inventorying number", and now he wants to the find out which numbers is self-inventorying , or leads to a self-inventorying number through iterated application of the inventorying operation described. You are been hired to help him in his investigations.

Given any non-negative integer n, it inventory is another integer consisting of a concatenation of integers c1 d1 C2 d2. .. CK DK, where each CI and di are an unsigned integer, every CI is positive, the di satisfy 0<=d1<d2<...<dk<= 9, and, for each digit D so appears anywhere in N, D equals di for some I and D occurs exactly CI times in the decimal R Epresentation 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 was called self-inventorying if n equals its inventory. It is called self-inventorying after J steps (J>=1) if J was the smallest number such that the value of the j-th Iterati ve application of the inventory function is self-inventorying. For instance, 21221314 was self-inventorying after 2 steps, since the inventory of 21221314 was 31321314, the inventory of 3 1321314 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 this 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 Itera tive application. For instance, 314213241519 enters a inventory loop of length 2, since the inventory of 314213241519 is 412223241519 and T He inventory of 412223241519 is 314213241519, the original number (we had j = 0 in this case).

Write a program that would read a sequence of non-negative integers and, for each input value, state whether it is SELF-INV Entorying, self-inventorying After J steps, enters a inventory loop of length k, or has none of the these properties after 15 Iterative applications of the inventory function.

Input

A sequence of non-negative integers, each has at most a digits, and followed by the terminating value-1. There is no extra leading zeros.

Output

For each non-negative input value n, the output of 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 is classified after iterations

Sample Input

22 31123314 314213241519) 21221314 111222234459-1

Sample Output

Source

East Central North America 1998

There is not much skill in the subject, that is, test programming ability.

Some of the areas of attention are:

1 Number-The basic algorithm, a lot of topics will be used, the topic Use hash table to calculate the number of occurrences of each number can be, there is a pit: note more than 9 number, multi-digit conversion to a string

2 map, of course, can use the STL, if the direct manual implementation, or using the trie algorithm implementation, then the difficulty of the subject greatly increased.

3 Simple calculation problems and read clearly test instructions, such as the requirement is greater than 15 steps, you need to deal with the extra, accidentally dropped in the pit.


The following procedures are marked error-prone or very easy to let people fall into the pit. Get in the pit.-_-| | |

#include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include < iostream> #include <string> #include <limits.h> #include <stack> #include <queue> #include <set> #include <map>using namespace std;string countnum (string &s) {int hashnum[10] = {0};int n = (int) s.si Ze (); for (int i = 0; i < n; i++) {if (S[i] < ' 0 ' | | s[i] > ' 9 ') break;hashnum[s[i]-' 0 ']++;} string rs;for (int i = 0; i <; i++) {if (Hashnum[i]) {int J = hashnum[i];string t;//easy to fault: when there are multiple digits while (j) T + = char (j %10 + ' 0 '), J/= 10;reverse (T.begin (), T.end ()); t + = char (i + ' 0 '); rs + = t;}} return RS;} Template<typename T1, TypeName t2>inline bool Equ (T1 t1, T2 T2) {return T1 = = (T1) T2;} int main () {string Num;while (Cin>>num &&!equ (num[0], '-')) {int c = 0;string N = num;map<string, int> Maps_i;while (True) {Maps_i[num] = c;string t = countnum (num), if (equ (t, num))//self-inventory{if (equ (0, c)) {cout< <n<< "is self-inventorying\n";} else{cout<<n<< "is self-inventorying after" <<c<< "steps\n";} break;} else if (Maps_i.count (t))//loop-inventory{int L = c-maps_i[t] + 1;//Easy fault: What ' s loop of length?! cout<<n<< "enters an inventory loop of length" <<L<< "\ n"; break;} else if (c >= 14)//Easy error: Write 15 is wrong! {cout<<n<< "Can not is classified after iterations\n"; num = t;c++;}} return 0;}






POJ 1016 Numbers that Count simulation topic

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.