-
Notifications
You must be signed in to change notification settings - Fork 0
/
AIBOHP.cpp
60 lines (46 loc) · 1.07 KB
/
AIBOHP.cpp
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
/*
Author: Srikanth Mujjiga (smujjiga)
Date: 25.Feb.2015
Problem: 1021. Aibohphobia http://www.spoj.com/problems/AIBOHP/
Problem code: AIBOHP
Type: O(n^2) memoriesed DP
*/
#include <stdio.h>
#ifdef _WIN32
inline int getchar_unlocked() { return getchar(); }
inline int putchar_unlocked(char c) { return putchar(c); }
#endif
int min(int i,int j) {
return i<j?i:j;
}
int mem[6110][6110];
int shortPalindrome(char *str, int i, int j) {
if (i>j) return 0;
if (mem[i][j] != -1) return mem[i][j];
if (str[i] == str[j])
return shortPalindrome(str,i+1,j-1);
else {
return (mem[i][j] = 1 + min(shortPalindrome(str,i+1,j), shortPalindrome(str,i,j-1)));
}
}
int main_AIBOHP() {
int t;
scanf("%d",&t);
char str[6110];
char c;
while(t--) {
int n=0;
c = 0;
while (c < 33)
c = getchar_unlocked();
while(c != EOF && c != '\n') {
str[n++] = c;
c = getchar_unlocked();
}
for (int i=0;i<n;i++)
for (int j=0;j<n;j++)
mem[i][j] = -1;
printf("%d\n",shortPalindrome(str,0,n-1));
}
return 0;
}