Hello there guys, I was trying to create a program that would tell me in how many parts I have to split a given sequence of letters, so every piece is a hum... forgot the word (what are these sequences called? aca, baab, dcacd).
I was able to make it in java, but it's a bit slow, so I searched around a bit and found the solution in C. Problem is, I don't know anything about C yet, so could someone transform this code in java for me?
Code:
/* pal.c */
/* OBI2004 */
/* Ulisses */
#include <stdio.h>
#define NMAX 2002
#define INF NMAX*NMAX
char str[NMAX], pal[NMAX][NMAX];
int n, partes[NMAX];
int main(void) {
int i, j, smaller, val, teste = 1;
while (scanf("%d", &n)==1 && n) {
scanf(" %s", str);
memset(pal, 0, sizeof(pal));
for (i=0; i<n; i++)
pal[i][i] = 1;
for (j=1; j<n; j++)
for (i=j-1; i>=0; i--)
pal[i][j] = ((str[i]==str[j]) && (i+1>=j-1 || pal[i+1][j-1]));
for (j=0; j<n; j++) {
if (pal[0][j]) {
parts[j] = 1;
} else {
smaller = INF;
for (i=1; i<=j; i++) {
if (pal[i][j] && parts[i-1]+1<smaller)
smaller = parts[i-1]+1;
}
parts[j] = smaller;
}
}
printf("Test %d\n", test++);
printf("%d\n\n", parts[n-1]);
}
return 0;
}