zoukankan      html  css  js  c++  java
  • D

      

                                         Palindrome Partitioning

    1000ms
    32768KB
     

    A palindrome partition is the partitioning of a string such that each separate substring is a palindrome.

    For example, the string "ABACABA" could be partitioned in several different ways, such as {"A","B","A","C","A","B","A"}, {"A","BACAB","A"}, {"ABA","C","ABA"}, or {"ABACABA"}, among others.

    You are given a string s. Return the minimum possible number of substrings in a palindrome partition of s.

    Input

    Input starts with an integer T (≤ 40), denoting the number of test cases.

    Each case begins with a non-empty string s of uppercase letters with length no more than 1000.

     

    Output

    For each case of input you have to print the case number and the desired result.

     

    Sample Input

    Sample Input

    Output for Sample Input

    3

    AAAA

    ABCDEFGH

    QWERTYTREWQWERT

    Case 1: 1

    Case 2: 8

    Case 3: 5

     

     

    此提找到一种代码精简的方法!!不容易啊,虽然1==Ms但是代码短了很多,也易于理解。。。。。。

     1 #include<stdio.h>
     2 #include<string.h>
     3 #define N 1010
     4 #include <algorithm>
     5 using namespace std;
     6 char a[N];
     7 int f[N];
     8 int pali (int n,int m)
     9 {
    10     int i,j;
    11     for(i=n,j=m;i<=(n+m)/2;i++,j--)
    12     if(a[i]!=a[j])return 0;
    13     return 1;
    14 }
    15 int main ()
    16 {
    17     int T,i,j,a_len,ca;
    18         scanf("%d",&T);
    19         for(ca=1;ca<=T;ca++)
    20         {
    21             scanf("%s",&a);
    22             a_len=strlen(a);
    23             for(i=0;i<a_len;i++)
    24             { f[i]=i+1;
    25               for(j=0;j<=i;j++)
    26               if(pali(j,i))
    27               f[i]=min(f[i],f[j-1]+1);
    28             } 
    29             printf("Case %d: ",ca);
    30             printf("%d
    ",f[a_len-1]);
    31         }
    32 }
  • 相关阅读:
    Web APIs——DOM
    案例:动态生成表格
    案例:简单版发布留言功能
    案例:下拉菜单功能
    案例:tab栏切换功能(原生JS写法)
    时间复杂度
    ubuntu下安装LNMP
    btree索引和hash索引的区别
    StandardServer.await: Invalid command 'GET / HTTP/1.1' received
    MySQL----触发器
  • 原文地址:https://www.cnblogs.com/ace-top/p/3279160.html
Copyright © 2011-2022 走看看