poj 1016 Numbers That Count【字符串】
题目大意:给你一个数字串,最大长度80,然后计算里面每个数字出现的次数,按照从小到大的顺序排列成另一个数字串。比如5553141变化后是21 13 14 35(2个1,1个3,1个4,3个5)。
如果1次变化后,数字串没变,那么输出“n is self-inventorying”其中,n代表题目给你的那个字符串。比如31123314 按照规则变化后还是31123314,所以输出“31123314 is self-inventorying ”。
如果 J 次变化后,字符串变成了一个self-inventorying 那么输出“n is self-inventorying after j steps”其中,n代表题目给你的那个字符串,j 就是变化次数了;比如给的是 21221314 第一次变化得到 31321314 ,再次变化得到 31123314,而31123314再次变化还是31123314,所以他是一个self-inventorying,所以变化了2次,输出 “21221314 is self-inventorying after 2 steps ”。
如果J次变化后,字符串串和前面某个字符串相同,也就是说形成了一个环,则输出环的长度。例如 314213241519 变化后是412223241519,再次变化是 314213241519,和第一个字符串重复了,所以环的长度是2。输出“314213241519 enters an inventory loop of length 2”
如果15次变化后还是没有出现上面的情况,输出“n can not be classified after 15 iterations”,n代表原串。
#include<stdio.h> #include<string.h> #include<string> #include<iostream> #include<map> #include<stdlib.h> using namespace std; string str[20]; map<char, int> mp; map<char, int>::iterator it; void change(int i) { str[i+1].erase(); mp.clear(); int len = str[i].length(); for(int j = 0; j < len; j++) { mp[ str[i][j] ]++; } for(it = mp.begin(); it != mp.end(); it++) { char s[10]; // itoa(it->second, s, 10); sprintf(s, "%d", it->second); str[i+1] += s; str[i+1] += it->first; } } int main() { while(cin >> str[0]) { if(str[0] == "-1") break; change( 0 ); if(str[1] == str[0]) { cout << str[0] << " is self-inventorying\n"; continue; } int step = 1; bool bl = 0; int tot; while(1) { change( step ); if(str[step] == str[step-1]) { bl = 0; break; } if(!bl) { for(int i = 0; i < step; i++) { if(str[step] == str[i]) { tot = step - i; bl = 1; break; } } } step++; if(step > 15) break; } if(step > 15 && !bl) { cout << str[0] << " can not be classified after 15 iterations\n"; } else if( bl ) { cout << str[0] << " enters an inventory loop of length " << tot <<endl; } else { cout << str[0] << " is self-inventorying after " << step-1 << " steps\n"; } } return 0; }
测试数据:
输入:
22
31123314
314213241519
21221314
111222234459
123456789
654641656
2101400052100005496
10000000002000000000
333
1
99999999999999999999999999999999999999999999999999999999999999999999999999999999
0000
0001
0111
1111
123456789
456137892
123213241561
543265544536464364
5412314454766464364
543267685643564364
5423434560121016464364
-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
123456789 is self-inventorying after 5 steps
654641656 enters an inventory loop of length 2
2101400052100005496 enters an inventory loop of length 2
10000000002000000000 enters an inventory loop of length 3
333 is self-inventorying after 11 steps
1 is self-inventorying after 12 steps
99999999999999999999999999999999999999999999999999999999999999999999999999999999 can not be classified after 15 iterations
0000 enters an inventory loop of length 2
0001 is self-inventorying after 8 steps
0111 is self-inventorying after 8 steps
1111 is self-inventorying after 8 steps
123456789 is self-inventorying after 5 steps
456137892 is self-inventorying after 5 steps
123213241561 enters an inventory loop of length 2
543265544536464364 enters an inventory loop of length 2
5412314454766464364 is self-inventorying after 3 steps
543267685643564364 enters an inventory loop of length 2
5423434560121016464364 is self-inventorying after 3 steps