File size: 1,180 Bytes
c4b0eef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
64
65
66
67
68
69
/*
****************************************************************
****************************************************************
-> Coded by Stavros Chryselis				   
-> Visit my github for more solved problems over multiple sites 
-> https://github.com/StavrosChryselis			  
-> Feel free to email me at [email protected]	
****************************************************************
****************************************************************
*/

#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#define gc() getchar_unlocked()
 
using namespace std;
int DP[6107][6107];
char str[6107];
int N;
 
void init()
{
	int i = 0;
	char c;
 
	c=gc();
	while(isspace(c))
		c=gc();
 
	while(!isspace(c) && c!=EOF)
	{
		str[i++] = c;
		c = gc();
	}
 
	N = i;
}
 
int main()
{
	int T, i, j, L;
 
	//freopen("input.txt","r",stdin);
	scanf("%d", &T);
 
	while(T--)
	{
		init();
 
		for(i=0; i<N; i++)
			DP[i][i] = 0;
 
		for(L = 2; L<=N; L++)
			for(i=0; i + L - 1 < N; i++)
			{
				j = i + L - 1;
				if(str[i] != str[j])
					DP[i][j] = 1 + min(DP[i+1][j], DP[i][j-1]);
				else
					DP[i][j] = DP[i+1][j-1];
			}
 
		printf("%d\n", DP[0][N-1]);
	}
 
	return 0;
}