题意与解析

这题其实特别简单,求给定排列的后继。使用stl(next_permutation)可以方便地解决这个问题。但是,想要自己动手解就是另外一回事了。我的解法是从后往前找到第一个$a_i$比$a_j$小($i<j$)的,然后交换之,接下来i->end范围内重排序即可。

代码

  1. /* ***********************************************
  2. Author :Sam X
  3. Created Time :2018年01月16日 星期二 08时44分26秒
  4. File Name :uva146.cpp
  5. ************************************************ */
  6. #include <iostream>
  7. #include <cstdio>
  8. #include <cstring>
  9. #include <algorithm>
  10. #include <string>
  11. #include <cmath>
  12. #include <cstdlib>
  13. #include <vector>
  14. #include <queue>
  15. #include <set>
  16. #include <map>
  17. #include <algorithm>
  18. #include <iterator>
  19. using namespace std;
  20. typedef long long ll;
  21. typedef unsigned long long ull;
  22. int main()
  23. {
  24. //freopen("input.txt","r",stdin);
  25. //freopen("output.txt","w",stdout);
  26. string str;
  27. while(cin>>str)
  28. {
  29. bool ok=false;
  30. if(str[0]=='#') break;
  31. int len=str.length();
  32. for(int i=len-1;i>=1;--i)
  33. {
  34. for(int j=i-1;j>=0;--j)
  35. {
  36. if(str[j]<str[i])
  37. {
  38. ok=true;
  39. swap(str[i],str[j]);
  40. auto iter=str.begin();
  41. advance(iter,j+1);
  42. sort(iter,str.end());
  43. break;
  44. }
  45. }
  46. if(ok) break;
  47. }
  48. if(ok) cout<<str<<endl;
  49. else cout<<"No Successor"<<endl;
  50. }
  51. return 0;
  52. }

【个人训练】(UVa146)ID Codes的更多相关文章

  1. uva146 ID codes

    Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  2. UVa-146 - ID Codes(下一个排列)

    /* ID Codes It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...

  3. UVA-146 ID Codes

    It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exerc ...

  4. Brute Force & STL --- UVA 146 ID Codes

     ID Codes  Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...

  5. POJ 1146:ID Codes

    ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...

  6. UVA 146 ID Codes(下一个排列)

    C - ID Codes Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Statu ...

  7. poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6229   Accepted: 3737 Descript ...

  8. POJ 1146 ID Codes 用字典序思想生成下一个排列组合

    ID Codes Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 7644   Accepted: 4509 Descript ...

  9. POJ 1146 ID Codes (UVA146)

    // 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...

随机推荐

  1. 【luogu T34117 打油门】 题解

    王强怎么这么强啊 王强太强了 二维树状数组 #include <cstdio> #include <cstring> #include <iostream> #in ...

  2. Android学习笔记_58_清除手机应用程序缓存

    通过查看手机设置(setting)源代码,发现它里面获取应用大小和缓存大小是通过PackageManager里面的getPackageSizeInfo方法.然而此方法时私有的,因此通过反射调用此方法. ...

  3. CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)

    传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds me ...

  4. Extjs 4.2 panel 添加 click 事件及右键菜单

    listeners: { render: function(c) { c.body.on('click', function() { //TODO 添加点击事件功能 }); c.body.on('co ...

  5. c语言描述的数据结构的注意细节

    :顺序表使用基址来表示和存储 int *p; p=(int *)malloc(initsize*sizeof(int)); L—>p[x]=xx; :链表 在于除了更改数据还要更改前后与之关联的 ...

  6. linux网络相关配制及命令

    1.虚拟机配制 查看ip: ip addr 配制网卡(读者可以忽略): 编辑虚拟网络编辑器,修改子网IP 查看ip,输入ip addr 开启网络:ifup eth0  关闭网络:ifdown eth0 ...

  7. c# 分析SQL语句中的表操作

    最近写了很多方向的总结和demo.基本包含了工作中的很多方面,毕竟c#已经高度封装并且提供了很多类库.前面已经总结了博文.最近2天突然感觉前面的SQL分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...

  8. c c++面试----c工程开发之链接

    多数c语言的初学者对c工程开发过程各个阶段的作用理解不到位,而这方面的的知识又是实际开发过程中经常用到的技能点,所以就成为面试考察中一个重要的考察方面.例如:头文件的作用.头文件的内容:链接的作用和意 ...

  9. noip模拟赛 动态仙人掌(并查集,贪心)

    思路: 贪心+并查集 因为45‘,所以可以很方便的算出每个仙人掌的最晚起跳(左端点) 右端点自然也能出来 先按左端点排序 如果他右面的和他相交,就更新 用并查集维护这个更新的关系 更新的同时维护高就好 ...

  10. mybatis <fireach> 拼接sql语句 org.apache.ibatis.exceptions.PersistenceException: ### Error querying database. Cause: org.apache.ibatis.builder.BuilderException: Error evaluating expression 'in'. Cause:

    <select id="getUserIn" parameterType="QueryVo" resultMap="userMap"& ...