diff options
author | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2017-01-09 10:47:37 +0700 |
---|---|---|
committer | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2017-01-09 10:47:37 +0700 |
commit | 318184dbda0658d8afae6dd3c13a0718382726a1 (patch) | |
tree | 5a62259a03200cd77823431994fe3479e5d7461f /NTU/pali2.c | |
parent | 15b38076cc08d89024864252e99e10fe6f597196 (diff) | |
download | cp-318184dbda0658d8afae6dd3c13a0718382726a1.tar.gz |
Add NTU/{pali2,editpic}.c and update README.md
Diffstat (limited to 'NTU/pali2.c')
-rw-r--r-- | NTU/pali2.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/NTU/pali2.c b/NTU/pali2.c new file mode 100644 index 0000000..72c9105 --- /dev/null +++ b/NTU/pali2.c @@ -0,0 +1,68 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +short MIN_K, *P; + +void putnc(char *s, short len) +{ + short i; + + for (i = 0; i < len; i++) + putchar(s[i]); + putchar(10); +} + +char palin(char *s, short len) +{ + short i; + + for (i = 0; i < (len + 1) / 2; i++) + if (s[i] != s[len - i - 1]) + return 0; + + return 1; +} + +void foo(char *s, short len, short k, short *p) +{ + short i, j; + + if (palin(s, len)) { + MIN_K = k; + memcpy(P, p, k * sizeof(short)); + return; + } + + if (k >= MIN_K) + return; + + for (i = len - 1; i; i--) + if (palin(s, i)) { + p[k] = p[k - 1] + i; + foo(s + i, len - i, k + 1, p); + } +} + +int main() +{ + short n, i, *p; + char *s; + + scanf("%hd\n", &n); + s = malloc(n + 1); + fgets(s, n + 1, stdin); + + MIN_K = n; + P = malloc(n); + p = malloc(n); + p[0] = 0; + foo(s, n, 1, p); + + printf("%hd\n", MIN_K); + for (i = 1; i < MIN_K; i++) + putnc(s + P[i - 1], P[i] - P[i - 1]); + puts(s + P[MIN_K - 1]); + + return 0; +} |