1.


/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 146 ID Codes
* Link:
http://uva.onlinejudge.org/index.php?option=onlinejudge&page=show_problem&problem=82
*
* @author Bastian Hoecker
* @author Philipp Hauck Thalheim
* @version 1.0, 04/01/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.100
*/

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <list>
#include <vector>
#include <map>
#include <sstream>
#include <utility>
#include <set>
#include <math.h>
using namespace std;

int main()
{
while(1)
{
char sor[55];
gets(sor);
if(sor[0] == '#')
break;
if(!next_permutation(sor,sor+strlen(sor)))
{
puts("No Successor");
continue;
}
puts(sor);
}
return 0;
}

---------------------------------------------------




1.


/*
* ACM Contest training
* Problem: 146 - ID Codes
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=82
*
* @author Patrick Bedat, Philippe Brousse, Christoph Goettschkes
* @version 1.0, 11/14/2010
*
* Method : Lexicographical permutation
* Status : Accepted
* Runtime: 0.088
*/

import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;

import java.util.Arrays;

class Main
{
static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] agrs) throws IOException
{
String line = reader.readLine();
while (!line.equals("#")) {
char[] perm = line.toCharArray();
int length = perm.length;
int k = -1;
char kChar = '#';
for (int i = length - 2; i >= 0; i--) {
if (perm[i] < perm[i + 1]) {
k = i;
kChar = perm[i];
break;
}
}

if (k >= 0) {

int swapPos = 0;
for (int i = length - 1; i > k; i--) {
if (perm[i] > kChar) {
swapPos = i;
break;
}
}

perm[k] = perm[swapPos];
perm[swapPos] = kChar;

Arrays.sort(perm, k + 1, perm.length);
StringBuilder out = new StringBuilder();
for (char i : perm)
out.append(i);
System.out.println(out);
} else {
System.out.println("No Successor");
}


line = reader.readLine();
}
}
}