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
转载于:https://www.cnblogs.com/-hsz/archive/2012/07/30/2616062.html
poj 1016 Numbers That Count【字符串】相关推荐
- 成长轨迹44 【ACM算法之路 百炼poj.grids.cn】【字符串处理】【2799、2976、2975、2742】...
一次ac的就不说啥了.. 2799:浮点数格式 View Code 1 #include <stdio.h> 2 #include <string.h> 3 #include ...
- CodeForces 25BPhone numbers(简单的字符串模拟题目)
本题的题意如下:就是把每一串电话号码给分割开,分割为两个一组或者三个一组,所以笨渣渣心里想了好久好久,找出了一个比较笨的方法,就是对其进行分类讨论,看其对3取余后的结果是几,这样总共有三种可能性. A ...
- poj 3077 Rounders 【简单字符串处理】
题意:就是4舍5入到最近的数. 题意有些难理解... 代码: #include <stdio.h> #include <string.h> char s[10]; int ma ...
- Linux 环境下vs2015 qt,QT5.8.0+MSVC2015安装以及环境配置(不需要安装VS2015)
在 CSS 预编译器之后:PostCSS 提到css预编译器(css preprocessor),你可能想到Sass.Less以及Stylus.而本文要介绍的PostCSS,正是一个这样的工具:css ...
- POJ 超详细分类
POJ 各题算法 1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 ...
- POJ前面的题目算法思路【转】
1000 A+B Problem 送分题 49% 2005-5-7 1001 Exponentiation 高精度 85% 2005-5-7 1002 487-3279 n/a 90% 2005-5- ...
- Bailian2721 忽略大小写比较字符串大小(POJ NOI0107-16)【字符串】
问题链接:POJ NOI0107-16 忽略大小写比较字符串大小. 忽略大小写比较字符串大小 总时间限制: 1000ms 内存限制: 65536kB 描述 一般我们用strcmp可比较两个字符串的大小 ...
- poj题目详细分类及算法推荐题目
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
- ACM POJ 题目分类(完整整理版本)
DP: 1011 NTA 简单题 1013 Great Equipment 简单题 1024 Calendar Game 简单题 ...
最新文章
- 用树莓派打造世界上最小的“iMac”
- Python多线程(1)——介绍
- springboot起步配置和自动配置原理
- Modbus和RS485是什么关系
- WordPress解析之数据库
- 产品经理,如何建立自己独特的产品观?(转)
- 表示和描述-边界追踪
- 软件工程师如何笑着活下去?
- 随手记_重建的五花八门的点云地图效果(供娱乐)
- 在windows 2003系统上安装诺基亚pc套件的方法
- 读《三体》差点污了我的三观
- 大众点评数据爬虫思路[更新版]
- 简单的python画图代码_python opencv实现简易画图板
- 微软账号被锁定后的记录历程(已永久封禁)
- 仿微信联系人索引列表ListView
- petalinux 的rootfs文件系统放在SD 分区上
- python的自省与反射
- 【RHEL】RHEL 7.6 用户和组管理
- 计算机名称显示word作者,如何让word文档不显示作者名
- 3d打印热床的PEI/玻璃/晶格玻璃/柔性平台/弹簧钢板如何选择
热门文章
- 利用python进行数据分析 百度云-利用Python进行数据分析 原书第2版.pdf
- python培训深圳-深圳哪里有Python培训?
- python基础教程怎么样-怎样学习Python?Python入门必看
- 用python的turtle画圆-PYTHON练习1-turtle画圆
- python能写软件吗-用什么软件写python
- python自带的shell是什么-python shell是什么东西
- python爬虫能干什么-Python爬虫可以做什么?
- 在哪里能收到python实例代码-python实现网站微信登录的示例代码
- python代码需要背吗-python程序需要编译吗
- 自学python网站推荐-推荐自学python必入的神仙网站