• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            ACM PKU 1159 Palindrome 最簡單的動態規劃

            Palindrome 
            Time Limit:3000MS  Memory Limit:65536K 
            Total Submit:12142 Accepted:4213 
            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 


            Source 
            IOI 2000 



            分析: 
            動態規劃求解。 
            設ch[1]..ch[n]表示字符串1至n位,i為左游標,j為右游標 ,則i從n遞減,j從i開始遞增。 
            min[i][j]表示i和j之間至少需要插入多少個字符才能對稱,我們最終需要得到的值是min[1][n]. 
            則 
            if(ch[i]==ch[j]) 
                min[i][j]=min[i+1][j-1]; 
            else 
                 min[i][j] = 1 +  (min[i+1][j]和min[i][j-1]中的較小值); 


            另外,min[][]可以定義為short  而非 int,動態規劃算法通常緊缺memory 。
            什么?你說short 和 int是一樣的? 呵呵,兄弟,大概你看的是年代比較久遠的C語言教材吧~

             1#include "string.h"
             2#include "stdio.h"
             3 int min[5001][5001];
             4
             5int MIN(short a,short b)
             6{
             7    if(a>b)return b;
             8    else return a;
             9}

            10
            11int main()
            12{
            13    int n;
            14    int i,j;
            15    char ch[5001];
            16  
            17
            18    scanf("%d",&n);
            19    scanf("%s",ch+1);
            20    for(i=1;i<=n;i++)
            21        for(j=1;j<=n;j++)
            22            min[i][j]=0;
            23
            24    for(i=n-1;i>=0;i--)
            25       for(j=i;j<=n;j++)
            26           if(ch[i]==ch[j])min[i][j]=min[i+1][j-1];
            27           else min[i][j]= 1 + MIN(min[i+1][j],min[i][j-1]);
            28    printf("%d",min[1][n]);
            29   
            30return 0;
            31    
            32}

            posted on 2007-09-14 02:04 流牛ζ木馬 閱讀(1649) 評論(1)  編輯 收藏 引用

            評論

            # re: ACM PKU 1159 Palindrome 最簡單的動態規劃 2008-04-02 19:47 赫赫

            改為short min[5001][5001];  回復  更多評論   

            <2007年11月>
            28293031123
            45678910
            11121314151617
            18192021222324
            2526272829301
            2345678

            導航

            統計

            公告

            MY Email/MSN :mars1021@163.com QQ : 27402040 流牛ζ木馬

            常用鏈接

            留言簿(6)

            隨筆檔案

            相冊

            搜索

            最新隨筆

            最新評論

            閱讀排行榜

            評論排行榜

            久久99国产精品尤物| 久久精品国产72国产精福利| 久久久SS麻豆欧美国产日韩| 亚洲人成精品久久久久| 日本久久久久亚洲中字幕| 欧洲人妻丰满av无码久久不卡| 18岁日韩内射颜射午夜久久成人| 色综合久久夜色精品国产| 久久精品亚洲中文字幕无码麻豆| 国产精品免费看久久久香蕉| 婷婷久久久亚洲欧洲日产国码AV | 亚洲综合伊人久久大杳蕉| 亚洲精品乱码久久久久久按摩| 青青草国产精品久久久久| 欧美精品丝袜久久久中文字幕| 久久精品国产亚洲沈樵| 亚洲精品午夜国产va久久| 久久―日本道色综合久久| 欧美日韩久久中文字幕| 久久AAAA片一区二区| 2021少妇久久久久久久久久| 99蜜桃臀久久久欧美精品网站 | 麻豆成人久久精品二区三区免费| 久久一区二区三区99| 久久精品一区二区三区不卡| 久久99热只有频精品8| 亚洲国产成人精品91久久久| 欧美精品福利视频一区二区三区久久久精品 | 色播久久人人爽人人爽人人片aV| 久久国产一区二区| 久久99久久99精品免视看动漫| 亚洲国产另类久久久精品小说| 久久免费视频1| 亚洲国产精品一区二区三区久久 | 久久久91人妻无码精品蜜桃HD| 情人伊人久久综合亚洲| 丁香久久婷婷国产午夜视频| 精品免费久久久久国产一区| 久久人搡人人玩人妻精品首页 | 久久精品国产99国产精品| 国产91久久综合|