The problem is given a permutation of 1,2, …, n, generate the next permutation in lexicographic order. For example, for 2 3 1 4 the answer is 2 3 4 1.
My code is,
void read(int *l, int* size) {
char buf[2900];
*size = 0;
char *in = buf;
int *out = l;
//cin.sync();
cin.getline(buf, 2900);
do {
*out = 0;
while (*in >= '0' && *in <= '9') {
*out = *out * 10 + (*in - '0');
in++;
}
if (*in)
in++;
++out;
} while (*in);
*size = out - l;
}
int main() {
int N, K;
cin >> N >> K;
int in[K][N];
for (int i = 0; i < K; ++i) {
int s;
read(in[i], &s);
}
for (int i = 0; i < K; ++i) {
for (int j = N - 2; j >= 0; --j) {
int x = in[i][j];
int s = -1, v = 1000;
for (int k = j + 1; k < N; k++) {
if (in[i][k] > x && in[i][k] < v) {
s = k;
v = in[i][k];
}
}
if (s != -1) {
int c = in[i][s];
in[i][s] = x;
in[i][j] = c;
sort(in[i] + j + 1, in[i] + N);
break;
}
}
}
for (int i = 0; i < K; i++) {
for (int k = 0; k < N; k++)
cout << in[i][k] << " ";
cout << endl;
}
return 0;
}
As you can probably say from the code, I am a complete novice. It gives the right answer when I test, but the judge says that it gives wrong answers.
Help?
Thank you!