Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

ID Codes

146.c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if 0
abaacb
cbbaa
#

Sample output

ababac
No Successor
#endif

static int
compar(const void *a, const void *b) {
	return *(char*)a - *(char*)b;
}


static void
doit(char * str) {
	int i,j,k;
	char c;
	int done = 0;
	int len = strlen(str);

	for (i=len-2; i>=0; i--) {
		if (str[i] < str[i+1]) {
			k = i+1;
			for (j=i+1; j<len; j++) {
				if (str[j] > str[i] && str[j] < str[k]) {
					k = j;
				}
			}
			c = str[i];
			str[i] = str[k];
			str[k] = c;
			qsort(&str[i+1], len-i-1, 1, compar);

			/* TODO */
			done = 1;
			break;
		}
	}
	if (done) {
		printf("%s\n", str);
	} else {
		printf("No Successor\n");
	}
}

int
main(void) {
	char str[100];

	while (1) {
		if (scanf("%s", str) != 1) {
			break;
		}
		if (str[0]=='#') {
			break;
		}
		doit(str);
	}
	return 0;
}