题目大意:
描述
“Kronecker's Knumbers”是一家生产塑料数字的小公司,用于标牌(剧院帐篷,加油站价格显示等)。所有者和唯一员工Klyde Kronecker通过维护库存书来跟踪他使用的每种类型的数字。例如,如果他刚刚签了一个包含电话号码“5553141”的标志,他会在他的书的一栏中写下“5553141”号码,在下一栏中他会列出每个数字中有多少使用:两个1,一个3,一个4和三个5。 (不使用的数字不会出现在清单中。)他以浓缩形式编写库存,如下所示:“21131435”。
“Kronecker's Knumbers”是一家生产塑料数字的小公司,用于标牌(剧院帐篷,加油站价格显示等)。所有者和唯一员工Klyde Kronecker通过维护库存书来跟踪他使用的每种类型的数字。例如,如果他刚刚签了一个包含电话号码“5553141”的标志,他会在他的书的一栏中写下“5553141”号码,在下一栏中他会列出每个数字中有多少使用:两个1,一个3,一个4和三个5。 (不使用的数字不会出现在清单中。)他以浓缩形式编写库存,如下所示:“21131435”。
前几天,Klyde填写了订单号码31123314,并惊讶地发现这个号码的库存与号码相同---它有三个1,一个2,三个3和一个4!他称这是一个“自我盘点号码”的例子,现在他想知道哪些号码是自我盘点的,或者通过下面描述的盘点操作的迭代应用导致自我盘点号码。你被雇用来帮助他进行调查。
给定任何非负整数n,其库存是另一个整数,由整数c1 d1 c2 d2 ... ck dk的串联组成,其中每个ci和di是无符号整数,每个ci为正,di满足0 <= d1 <d2 <... <dk <= 9,并且,对于出现在n中任何位置的每个数字d,对于某些i,d等于di,并且在n的十进制表示中d恰好出现ci次。例如,为了计算5553141的库存,我们设置c1 = 2,d1 = 1,c2 = 1,d2 = 3等,给出21131435.数字1000000000000具有库存12011(“十二0,一个1”)。
如果n等于其库存,则整数n称为自库存。如果j是最小数字,则j步骤(j> = 1)之后称为自动清点,使得库存函数的第j次迭代应用的值是自我清点的。例如,21221314在两步后自动盘点,因为21221314的库存为31321314,库存31321314为31123314,而31123314为自库存。
最后,如果k是最小数,则n进入长度为k(k> = 2)的库存循环,使得对于某些整数j(j> = 0),库存函数的第j次迭代应用的值是与第(j + k)次迭代应用的值相同。例如,314213241519进入长度为2的库存循环,因为314213241519的库存为412223241519,而库存412223241519为原始编号314213241519(在这种情况下我们有j = 0)。
编写一个程序,它将读取一系列非负整数,并且对于每个输入值,说明它是自动盘点,j步之后的自动盘点,进入长度为k的库存循环,还是没有这些属性之后库存功能的15次迭代应用。
输入
一系列非负整数,每个最多包含80个数字,后跟终止值-1。没有额外的前导零。
输出
对于每个非负输入值n,从以下消息中输出适当的选择(其中n是输入值,j是正整数,k是大于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
样例输入
22
31123314
314213241519
21221314
111222234459
-1
样例输出
输入
一系列非负整数,每个最多包含80个数字,后跟终止值-1。没有额外的前导零。
输出
对于每个非负输入值n,从以下消息中输出适当的选择(其中n是输入值,j是正整数,k是大于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
样例输入
22
31123314
314213241519
21221314
111222234459
-1
样例输出
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
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
来源:https://blog.csdn.net/baoqiaoben/article/details/80085965
#include <iostream>
#include <sstream>
#include <iomanip>
#include <string>
#include <numeric>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <bitset>
#include <utility>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const int MAXN = 100;
const int MOD7 = 1000000007;
const int MOD9 = 1000000009;
const int INF = 2000000000;//0x7fffffff
const double EPS = 1e-9;
const double PI = 3.14159265358979;
const int dir_4r[] = { -1, 1, 0, 0 };
const int dir_4c[] = { 0, 0, -1, 1 };
const int dir_8r[] = { -1, -1, -1, 0, 0, 1, 1, 1 };
const int dir_8c[] = { -1, 0, 1, -1, 1, -1, 0, 1 };
int cnt[MAXN];
char digits[MAXN];
char buf1[MAXN];
char buf2[MAXN];
bool loop;//判断是否出现环
int loopsize;
map<string, int> mp;
void fun(char *str, int &index, int cnt) {
if (cnt == 0)
return;
int d = cnt % 10;
cnt /= 10;
fun(str, index, cnt);
str[index++] = d + '0';
}
void inventory(char *in, char *out) {
memset(cnt, 0, sizeof(cnt));
for (int i = 0; in[i]; ++i)
cnt[in[i] - '0']++;
int index = 0;
for (int i = 0; i < 10; ++i) {
if (cnt[i]) {
fun(out, index, cnt[i]);
out[index++] = i + '0';
}
}
out[index++] = '\0';
}
bool isSelfInventory(char *buf1, char *buf2) {
inventory(buf1, buf2);
if (strcmp(buf1, buf2) == 0)
return true;
else
return false;
}
int solve() {
int index = 0;
map<string, int>::iterator it;
mp.clear();
loop = false;
loopsize = INF;
strcpy(buf1, digits);
while (index <= 15) {
if (isSelfInventory(buf1, buf2))
break;
it = mp.find(string(buf1));
if (it != mp.end()) {
loop = true;
loopsize = min(loopsize, index - it->second);
}
mp[string(buf1)] = index++;
strcpy(buf1, buf2);
}
return index;
}
int main() {
while (scanf("%s", digits)) {
if (digits[0] == '-')
break;
int ans = solve();
if (ans == 0)
printf("%s is self-inventorying\n", digits);
else if (ans <= 15)
printf("%s is self-inventorying after %d steps\n", digits, ans);
else if (loop)
printf("%s enters an inventory loop of length %d\n", digits, loopsize);
else
printf("%s can not be classified after 15 iterations\n", digits);
}
//system("pause");
return 0;
}