-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplain.c
63 lines (59 loc) · 1.09 KB
/
plain.c
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<stdio.h>
#include<string.h>
#define STLEN 1001
#define NUM 1000
#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* s)
{
int len = strlen(s);
if (len <= 1) { return 1; }
int start = 0;
int maxlen = 0;
for (int i = 1; i < len; i++)
{
int low = i - 1;
int high = i;
while (low >= 0 && high < len && s[low] == s[high])
{
low--;
high++;
}
if (high - low - 1 > maxlen)
{
maxlen = high - low - 1;
start = low + 1;
}
low = i - 1; high = i + 1;
while (low >= 0 && high < len && s[low] == s[high])
{
low--;
high++;
}
if (high - low - 1 > maxlen)
{
maxlen = high - low - 1;
start = low + 1;
}
}
return maxlen;
}