【个人训练】(UVa146)ID Codes
题意与解析
这题其实特别简单,求给定排列的后继。使用stl(next_permutation)可以方便地解决这个问题。但是,想要自己动手解就是另外一回事了。我的解法是从后往前找到第一个$a_i$比$a_j$小($i<j$)的,然后交换之,接下来i->end范围内重排序即可。
代码
/* ***********************************************
Author :Sam X
Created Time :2018年01月16日 星期二 08时44分26秒
File Name :uva146.cpp
************************************************ */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <iterator>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
int main()
{
//freopen("input.txt","r",stdin);
//freopen("output.txt","w",stdout);
string str;
while(cin>>str)
{
bool ok=false;
if(str[0]=='#') break;
int len=str.length();
for(int i=len-1;i>=1;--i)
{
for(int j=i-1;j>=0;--j)
{
if(str[j]<str[i])
{
ok=true;
swap(str[i],str[j]);
auto iter=str.begin();
advance(iter,j+1);
sort(iter,str.end());
break;
}
}
if(ok) break;
}
if(ok) cout<<str<<endl;
else cout<<"No Successor"<<endl;
}
return 0;
}
【个人训练】(UVa146)ID Codes的更多相关文章
- uva146 ID codes
Description It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...
- UVa-146 - ID Codes(下一个排列)
/* ID Codes It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In or ...
- UVA-146 ID Codes
It is 2084 and the year of Big Brother has finally arrived, albeit a century late. In order to exerc ...
- Brute Force & STL --- UVA 146 ID Codes
ID Codes Problem's Link:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&a ...
- POJ 1146:ID Codes
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6281 Accepted: 3769 Description ...
- UVA 146 ID Codes(下一个排列)
C - ID Codes Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Statu ...
- poj 1146 ID Codes (字符串处理 生成排列组合 生成当前串的下一个字典序排列 【*模板】 )
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6229 Accepted: 3737 Descript ...
- POJ 1146 ID Codes 用字典序思想生成下一个排列组合
ID Codes Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7644 Accepted: 4509 Descript ...
- POJ 1146 ID Codes (UVA146)
// 求下一个排列// 如果已经是最后一个排列// 就输出 No Successor// stl 或 自己写个 生成排列 我测试了下 两个速率是一样的.只是代码长度不同 /* #include < ...
随机推荐
- 【luogu T34117 打油门】 题解
王强怎么这么强啊 王强太强了 二维树状数组 #include <cstdio> #include <cstring> #include <iostream> #in ...
- Android学习笔记_58_清除手机应用程序缓存
通过查看手机设置(setting)源代码,发现它里面获取应用大小和缓存大小是通过PackageManager里面的getPackageSizeInfo方法.然而此方法时私有的,因此通过反射调用此方法. ...
- CodeForces - 598C Nearest vectors(高精度几何 排序然后枚举)
传送门: http://codeforces.com/problemset/problem/598/C Nearest vectors time limit per test 2 seconds me ...
- Extjs 4.2 panel 添加 click 事件及右键菜单
listeners: { render: function(c) { c.body.on('click', function() { //TODO 添加点击事件功能 }); c.body.on('co ...
- c语言描述的数据结构的注意细节
:顺序表使用基址来表示和存储 int *p; p=(int *)malloc(initsize*sizeof(int)); L—>p[x]=xx; :链表 在于除了更改数据还要更改前后与之关联的 ...
- linux网络相关配制及命令
1.虚拟机配制 查看ip: ip addr 配制网卡(读者可以忽略): 编辑虚拟网络编辑器,修改子网IP 查看ip,输入ip addr 开启网络:ifup eth0 关闭网络:ifdown eth0 ...
- c# 分析SQL语句中的表操作
最近写了很多方向的总结和demo.基本包含了工作中的很多方面,毕竟c#已经高度封装并且提供了很多类库.前面已经总结了博文.最近2天突然感觉前面的SQL分析阻组件的确麻烦,也注意看了下.为了方便大家学习 ...
- c c++面试----c工程开发之链接
多数c语言的初学者对c工程开发过程各个阶段的作用理解不到位,而这方面的的知识又是实际开发过程中经常用到的技能点,所以就成为面试考察中一个重要的考察方面.例如:头文件的作用.头文件的内容:链接的作用和意 ...
- noip模拟赛 动态仙人掌(并查集,贪心)
思路: 贪心+并查集 因为45‘,所以可以很方便的算出每个仙人掌的最晚起跳(左端点) 右端点自然也能出来 先按左端点排序 如果他右面的和他相交,就更新 用并查集维护这个更新的关系 更新的同时维护高就好 ...
- 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"& ...