POJ 1016 不断压缩字符串判断三种结果
来源:互联网 发布:sql server 2008 费用 编辑:程序博客网 时间:2024/05/16 06:52
Numbers That Count
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20129 Accepted: 6735
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20129 Accepted: 6735
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
意思就是分为那几种情况,用递归不断压缩
用是s【n】【i】,n表示压缩的次数,n=0即为原串,在s【n】下从s【n】【0】到s【n】【'\n'】都是压缩后的串
#include <iostream>#include <cstdio>#include <cstring>using namespace std;char s[16][85];int turn,k;void deal(int n,int t){ int i,num2,num1; int time[10]={0}; if(t<=15) { for(i=0;i<strlen(s[n]);i++) time[s[n][i]-'0']++; k=0; for(i=0;i<=9;i++) { if(time[i]>9) { num2=time[i]/10; num1=time[i]%10; s[t][k++]=num2+'0'; s[t][k++]=num1+'0'; s[t][k++]=i+'0'; } else if(time[i]>0) { s[t][k++]=time[i]+'0'; s[t][k++]=i+'0'; } } s[t][k]='\0'; //很关键,表示这个字符串到头了,不在去读了,听阳哥说string类型会自动加上结束符 deal(t,t+1); }}int main(){ int i,j; int flag1,flag2,flag3; while( scanf("%s",s[0])!=EOF && s[0][0]!='-') { flag1=0; flag2=0; flag3=0; i=0,j=0; turn=1; deal(0,turn); if(strcmp(s[0],s[1])==0) { printf("%s is self-inventorying\n",s[0]); flag1=1; } if(!flag1) { for(i=1;i<15;i++) { if(strcmp(s[i],s[i+1])==0) { printf("%s is self-inventorying after %d steps\n",s[0],i); flag2=1; break; } } if(!flag2) { for(j=1;j<16;j++) { for(i=0;i<=j-2;i++) { if(strcmp(s[j],s[i])==0) { printf("%s enters an inventory loop of length %d\n",s[0],j-i); flag3=1; break; } } if(flag3) break; } } } if(!flag1 && !flag2 && !flag3) { printf("%s can not be classified after 15 iterations\n",s[0]); } } return 0;}
//用string还是简化了一些。。。。#include <iostream>#include <cstring>using namespace std;int main(){ int i,j,l,k,n,a[15]= {0,0},f; string c[20];//c[0]为输入的字符串 char b[20]; while (cin>>c[0]) { if (c[0]=="-1") break; cout<<c[0]; for (n=0; n<15; n++) { l=c[n].length(); k=0; for(i=0; i<l; i++) { j=c[n].at(i)-'0'; a[j]++; } for(i=0; i<10; i++) if (a[i]) { if (a[i]>=10) b[k++]=(a[i] / 10)+'0'; b[k++]=(a[i] % 10)+'0'; a[i]=0; b[k++]=i+'0'; b[k]='\0'; c[n+1]=string(b);//c[n+1]即为压缩的新串 } if ((n==0&&c[0]==c[1])||c[n]==c[n+1])//两种情况 先输出self-inventorying { cout<<" is self-inventorying"; if (n==0) cout<<endl; else cout<<" after "<<n<<" steps"<<endl; break; } f=0; for(i=n; i>=0; i--) if (c[i]==c[n+1])//当前新串压缩到c[n+1],将新串和旧串依次比较 { cout<<" enters an inventory loop of length "<<n+1-i<<endl; f=1; break; } if (f==1) break; if (n==14) cout<<" can not be classified after 15 iterations"<<endl; } } return 0;}
0 0
- POJ 1016 不断压缩字符串判断三种结果
- poj 3332-判断字符串是否合法
- POJ 3259 Wormholes(三种方法判断负环)
- Java判断字符串是否为空的三种方法
- C#判断空字符串的三种方法
- C#判断空字符串的三种方法
- Java 判断字符串是否为空的三种方法.
- 判断字符串是不是数字的三种方法
- Java 判断字符串是否为空的三种方法
- fn contains 包含字符串 三种方法判断
- Java 判断字符串是否为空的三种方法.
- 判断Java空字符串三种方法的比较
- 判断Java空字符串三种方法的比较
- shell判断字符串包含关系的三种方法
- 判断两个由大小写字母和空格组成的字符串在忽略大小写和压缩掉空格后是否相等?--POJ--字符串处理
- [POJ]3219判断组合数模2结果的奇偶
- POJ 1575字符串的条件判断
- NSString字符串相加连接的三种方法、判断是否包含字符串的方法
- MySQL存储过程
- Hive的安装部署、测试、功能架构、表数据加载、导出、查询
- clearInterval只终止定时器不终止函数
- JAVA中double类型运算结果异常的解决
- MCGS保存初值问题
- POJ 1016 不断压缩字符串判断三种结果
- POJ-1995 Raising Modulo Numbers
- MySQL触发器应用
- 【HDU】3501 - Calculation 2(欧拉函数,互质数之和公式)
- 1011. A+B和C (15)
- MySQL的事件
- Performance benchmarks: KVM vs. Xen
- 今天英树入驻CSDN啦
- Leetcode 111. Minimum Depth of Binary Tree (Easy) (cpp)