1.

/**
 * Angewandte Mathematik, SS11
 * Problem: 146 - ID Codes
 * Link: http://uva.onlinejudge.org/external/1/146.pdf
 *
 * @author Westerfield, Christopher


 * @author Gent Selmanaj
 * @author Marco Schuster
 * @version 1.0, 05/03/2011
 *
 * Method : Ad-Hoc
 * Status : Accepted
 * Runtime: 0.140
 */


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


import java.io.InputStreamReader;

import java.util.Arrays;

class Main
{
    static BufferedReader leser;
    public static void main(String[] agrs) throws IOException
    {
        leser = new BufferedReader(new InputStreamReader(System.in));


            //Zeile einlesen
            String zeile = leser.readLine();
            //Nicht erlaubtes Zeichen
            while (!zeile.equals("#")) {
                //Zeichenkette aufteilen
                char[] zulassung = zeile.toCharArray();


                int tmp = -1;
                char zeichen = '#';
                //Zeichen Ketten Durchlauf - Abgleich
                for (int i = zulassung.length - 2; i >= 0; i--) {
                    //Zeichen Ketten erkennen.


                    if (zulassung[i] < zulassung[i + 1]) {
                        tmp = i;
                        zeichen = zulassung[i];
                        break;
                    }
                }


                //Wenn tmp größer 0
                if (tmp >= 0) {
                    int wechsel = 0;
                    //Zeichenkette nochmals durchlaufen lassen
                    for (int i = zulassung.length - 1; i > tmp; i--) {


                        if (zulassung[i] > zeichen) {
                            wechsel = i;
                            break;
                        }
                    }
                   

                    zulassung[tmp] = zulassung[wechsel];

                    zulassung[wechsel] = zeichen;
                    Arrays.sort(zulassung, tmp + 1, zulassung.length);
                    String rueckgabe = "";
                    //Rückgabe vorbereiten


                    for (char i : zulassung)
            rueckgabe+=i;
                    //Ausgabe
                    System.out.println(rueckgabe);
                } else {
                    //Ausgabe


                    System.out.println("No Successor");
                }
                //Nächste Zeile lesen
                zeile = leser.readLine();
            }
    }
}


2.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
* Angewandte Mathematik, SS11
* Problem 146 (n¦chste Permuation)
* Link: http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=27&page=show_problem&problem=2520
*
* @author Benedikt Z¨nnchen
* @author Erik Wenzel
* @version 1.0, 29/04/2011
*
* Method : Combinatorics, Memorizing
* Status : Accepted
* Runtime:
*/
public class Main
{
public static void main(String[] args) throws IOException
{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = "";
List<MyCharacter> cList;
while((line=reader.readLine()) != null)
{
if(line.equals("#"))
{
break;
}
cList = new ArrayList<MyCharacter>(line.length());
for(int i = 0; i < line.length(); i++)
{
cList.add(new MyCharacter(line.charAt(i)));
}
Permutation<MyCharacter> perm = new Permutation<MyCharacter>(cList);
cList = perm.nextPermutation();
if(cList != null)
{
perm.print();
System.out.println();
}
else
{
System.out.println("No Successor");
}
}
}

public static class MyCharacter implements Comparable<MyCharacter>
{
private char c;

public MyCharacter(char c)
{
this.c = c;
}

@Override
public int compareTo(MyCharacter obj)
{
int result = c - obj.c;
if(result < 0)
{
return -1;
}
else if(result > 0)
{
return 1;
}
else
{
return 0;
}
}

@Override
public String toString()
{
return c+"";
}

}

public static class Permutation<T extends Comparable<T>>
{
private List<T> list;
private long[] fak = new long[20];

public Permutation(List<T> list)
{
this.list = list;
}

public long rank()
{
int n = list.size();
int ni;
long rank = 0;
long fact = 1;

for(int i=n-2; i >= 0; i--)
{
// schrittweises aufbauen der Fakult¦t
fact *= n-i-1;
ni = 0;
// unsere Summenformel
for(int j = i+1; j < n; j++)
{
if(list.get(j).compareTo(list.get(i)) == -1)
{
ni++;
}
}
rank += ni*fact;
}
return rank;
}

/**
* generiert die n¦chste Permutation
*/
public List<T> nextPermutation()
{
if(list.size() <= 1)
{
return null;
}

// suche von rechts p_k > p_(k+1)
int pivot = getPivot();

if(pivot>=0)
{
// suche erneut von rechts p_t > p_k
int i = list.size() - 1;
while (i > 0 && list.get(pivot).compareTo(list.get(i)) > -1)
{
i--;
}
// vertausche p_k mit p_t
swap(pivot, i);

// drehe die Sequenz ab p_(k+1) bis zum ende um
Collections.reverse(list.subList(pivot + 1, list.size()));
}
else
{
return null;
}
return list;
}

private int getPivot()
{
int i = list.size() - 1;
while (i > 0 && list.get(i-1).compareTo(list.get(i)) > -1)
{
i--;
}
return --i;
}

/**
* Swaps two entries in a List<Entr>
* @param i position i
* @param j position j
* @param list Liste
*/
public void swap(int i, int j)
{
T tmp = list.get(i);
list.set(i, list.get(j));
list.set(j, tmp);
}

/**
* Vertauscht die Reihenfolge in einer Liste ab einem gewissen startindex
* @param <T> generischer Typ
* @param start startindex
* @param list Liste in der die Reihenfolge vertauscht wird
*/
public void reverse(int start)
{
for(int i = 0; i < list.size()-1-start; i++)
{
T tmp = list.get(start+i);
list.set(i+start, list.get(list.size()-1-i));
list.set(list.size()-1-i, tmp);
}
}

/**
* Gibt eine Liste aus
* @param <T> generischer Typ
* @param list Liste die ausgegeben wird
*/
public void print()
{
for (Comparable<T> comparable : list)
{
System.out.print(comparable);
}
}

public void unrank(long p)
{
long rank = p;
int n = list.size();
List<T> copy = new ArrayList<T>(n);

// Zielliste
List<T> target = new ArrayList<T>(n);

// generiere die Fakult¦t bis n
long[] fak = getFakArray(n);

// Hilfskonstrukt um festzustellen welche Elemente schon verwendet wurden
for(int i = 1; i <= n; i++)
{
copy.add(null);
}

// Schrittweises aufbauen der Elemente
for (int i = 0; i < n; i++)
{
int position = (int) (rank / fak[n-i-1]);
rank = rank % fak[n-i-1];

int freePosition = 0;
// †berspringe bereits bereits verwedete Elemente
while (position >= 0 )
{
if (copy.get(freePosition++) == null)
{
position--;
}
}

copy.set(freePosition-1, list.get(freePosition-1));
target.add(list.get(freePosition-1));
}
list = target;
}


/**
* calculate the all values from 0!>=n>=limt! and return them in an Array
* @param limit
* @return limit! Array
*/
public long[] getFakArray(int limit)
{
if(limit > 20)
{
throw new IllegalArgumentException();
}

fak[0]=1;
if(fak[limit] == 0)
{
for(int i = 1; i<=limit; i++)
{
fak[i] = i*fak[i-1];
}
}
return fak;
}





interface Permutable<P> extends Comparable<P>
{
int getValue();
P getData();
}
}
}


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



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();
}
}
}