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 }
  • 相关阅读:
    JAVA 一个接口多个实现类
    关于Web服务器
    美团买菜IOS版设备风控浅析与算法还原
    阿里App防Bot新版AliTigerTally方案浅析与算法还原1
    使用php的openssl_encrypt和python的pycrypt进行跨语言的对称加密和解密问题
    一个把人民币小写转换为大写中文的方法
    《重构》代码坏味道
    git 合并分支
    java中SPI机制 代码改变世界
    echo print print_r的区别
  • 原文地址:https://www.cnblogs.com/ace-top/p/3279160.html
Copyright © 2011-2022 走看看