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 }
  • 相关阅读:
    数字资产交易所记录
    How to decode input data from a contract transaction without ABI?
    【收藏】ETH以太坊各个环境的公共的RPC服务!!!
    Solidity知识点集 — 溢出和下溢
    docker run 与docker start的区别
    子网掩码计算192.168.1.0/24 24 / 11
    Solidity-让合约地址 接受ETH的转账充值的 三种方式
    echarts的散点图
    debug.js中的length的错误
    26个工具类
  • 原文地址:https://www.cnblogs.com/ace-top/p/3279160.html
Copyright © 2011-2022 走看看