-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlaindrome.c
More file actions
48 lines (45 loc) · 1023 Bytes
/
Copy pathPlaindrome.c
File metadata and controls
48 lines (45 loc) · 1023 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include<stdio.h>
#include<string.h>
#define STLEN 100
#define NUM 100
#define true 1
#define flase 0
typedef int bool;
int longestPalindrome(char *str);
int main(void){
int i = 0, n = 0;
int m[NUM] = {0};
char plain[NUM][STLEN];
while((scanf("%d", &n) != EOF) && (n != 0)){
scanf("%s", plain[i]);
m[i] = n;
i++;
}
for (int j = 0; j < i; j++){
char* p = plain[j];
int len = longestPalindrome(p);
m[j] = m[j] - len;
}
for (int j = 0; j < i; j++) printf("%d\n", m[j]);
return 0;
}
int longestPalindrome(char *str){
int len = strlen(str);
int lps[len][len];
for (int i = 0; i < len; i++){
for (int j = 0; j < len; j++){
if(i == j) lps[i][j] = 1;
else lps[i][j] = 0;
}
}
for (int i = 1; i < len; i++){
for (int j = 0; j + i < len; j++){
if (str[j] == str[i + j]) lps[j][i + j] = lps[j + 1][i + j - 1] + 2;
else{
if(lps[j][i + j - 1] < lps[j + 1][i + j]) lps[j][i + j] = lps[j + 1][i + j];
else lps[j][i + j] = lps[j][i + j - 1];
}
}
}
return lps[0][len - 1];
}