1159--Palindrome(dp:迴文串變形2),1159--palindromedp

來源:互聯網
上載者:User

1159--Palindrome(dp:迴文串變形2),1159--palindromedp
Palindrome

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 53431   Accepted: 18454

Description

A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into the string in order to obtain a palindrome. 

As an example, by inserting 2 characters, the string "Ab3bd" can be transformed into a palindrome ("dAb3bAd" or "Adb3bdA"). However, inserting fewer than 2 characters does not produce a palindrome. 

Input

Your program is to read from standard input. The first line contains one integer: the length of the input string N, 3 <= N <= 5000. The second line contains one string with length N. The string is formed from uppercase letters from 'A' to 'Z', lowercase letters from 'a' to 'z' and digits from '0' to '9'. Uppercase and lowercase letters are to be considered distinct.

Output

Your program is to write to standard output. The first line contains one integer, which is the desired minimal number.

Sample Input

5Ab3bd

Sample Output

2

Source

IOI 2000另一種dp的方法,dp[l][i],表示長度為l的以第i個字元開始的字串需要多少操作。對於長度為1的字串,操作為0長度為2的字串,如果兩個字元相同,操作為0,不同操作為1長度為3的字串,如果左右相同,操作為0,左右不同,對於a[1],a[2],a[3]來說 可以在前面增加a[3],或後面增加a[1],那麼就只需要判斷剩餘的兩和字元需要的操作了。長度為4的字串,左右相同,那麼需要求中間的兩個字元,不同的話和長度為3的判斷方式相同。得到 長度為l開始為i的串可以由, 長度為l-1開始為i的,長度為l-1開始為i+1的,或者是長度為l-2,開始為i+1的變化得到。推出dp公式
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int dp[3][5100] ;char str[5100] ;int main(){    int i , l , k1 , k2 , k3 , n ;    while(scanf("%d", &n) !=EOF)    {        scanf("%s", str);        for(i = n ; i >= 0 ; i--)            str[i] = str[i-1] ;        k1 = -1 ; k2 = 0 ; k3 = 1;        memset(dp,0,sizeof(dp));        for(l = 2 ; l <= n ; l++)        {            k3++ ;            if(k3 == 3) k3 = 0 ;            if(k3 == 0){ k2 = 2 ; k1 = 1 ; }            else if( k3 == 1 ){ k2 = 0 ; k1 = 2 ; }            else { k2 = 1 ; k1 = 0 ; }            for(i = 1 ; i <= n-l+1 ; i++)            {                if( str[i] == str[i+l-1] )                    dp[k3][i] = min( min(dp[k2][i]+1,dp[k2][i+1]+1),dp[k1][i+1] ) ;                else                    dp[k3][i] = min( dp[k2][i]+1 , dp[k2][i+1]+1);            }        }        printf("%d\n", dp[k3][1]);    }    return 0;}


1、代碼填空 形如:“abccba”,“abcba”的串稱為迴文串,下列代碼判斷一個串是否為迴文串補充空白的

char buf[] = "abcde11edcba";
int x = 1;
for(int i=0; i < strlen(buf) - 1 - i; i++)
{
if(buf[i] != buf[strlen(buf) - 1 - i]) {printf("不是迴文串");exit(0);}
}
printf("是迴文串");
好久沒寫c語言了,代碼可能有錯,希望思路能作參考
 
2.(40分)下面程式的功可以是:判斷一個字串是否為迴文串(迴文串指正讀反讀都一樣的字串,如:字串

【1】
#include "string.h"
#include "stdio.h"
void main()
{
char x[20];
int i,j,n;
gets(x);
n= strlen(x) ;
i=0;
j=n-1;
while(i<j)
{
if(x[i]!=x[j]) break ;
i++;
j-- ;
}
if(i>=j)
printf("The number is palindrome");
else printf("The number is not palindrome");
}
【2】
#include <stdio.h>
void main()
{
int i,a[10];
printf("input 10 numbers:\n");
for(i=0;i<10;i++)
scanf("%d",a[i]);
printf("\n");
for(i=0;i<=9;i++)
printf("%d ",a[i]);
printf("\n");
for(i=9;i<=0;i--)
printf("%d ",a[i]);
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.