UVA 11584 Partitioning by Palindromes

题意:把字符串划分成尽量少的回文串。

dp[i] = max{dp[j-1] + 1 | str[j....i]为回文串}。

View Code
 1 /*

 2 Author:Zhaofa Fang

 3 Lang:C++

 4 */

 5 #include <cstdio>

 6 #include <cstdlib>

 7 #include <sstream>

 8 #include <iostream>

 9 #include <cmath>

10 #include <cstring>

11 #include <algorithm>

12 #include <string>

13 #include <utility>

14 #include <vector>

15 #include <queue>

16 #include <stack>

17 #include <map>

18 #include <set>

19 using namespace std;

20 

21 typedef long long ll;

22 #define DEBUG(x) cout<< #x << ':' << x << endl

23 #define REP(i,n) for(int i=0;i < (n);i++)

24 #define FOR(i,s,t) for(int i = (s);i <= (t);i++)

25 #define PII pair<int,int>

26 #define PB push_back

27 #define MP make_pair

28 #define FI first

29 #define SE second

30 #define lowbit(x) (x&(-x))

31 #define INF (1<<30)

32 

33 char str[1010];

34 int dp[1010];

35 

36 bool check(char s[],int l,int r)

37 {

38     for(int i=l,j=r;i<=r;i++,j--)

39         if(s[i] != s[j])return false;

40     return true;

41 }

42 int main()

43 {

44 #ifndef ONLINE_JUDGE

45     freopen("in","r",stdin);

46 #endif

47     int T;

48     scanf("%d",&T);

49     while(T--)

50     {

51         scanf("%s",str+1);

52         int len = strlen(str+1);

53         REP(i,len+1)dp[i] = INF;

54         dp[0] = 0;

55         FOR(i,1,len)

56         {

57             FOR(j,1,i)

58             {

59                 if(check(str,j,i))

60                 {

61                     dp[i] = min(dp[i],dp[j-1] + 1);

62                 }

63             }

64         }

65         printf("%d\n",dp[len]);

66     }

67     return 0;

68 }

你可能感兴趣的:(partition)