• <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>

            poj1159

            Palindrome
            Time Limit: 3000MS Memory Limit: 65536K
            Total Submissions: 40443 Accepted: 13741

            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

            5
            Ab3bd

            Sample Output

            2
            這題跟編輯距離類似
            由于自己一點思路也沒有
            所以網上找了找題解

            方法1:f[i][j]表示從下標i到下標j最少添加幾個字符構成回文


                        f[i][j]=min(f[i+1][j-1],min(f[i][j-1],f[i+1][j])+1)

            方法2:求該串與其反串的最長公共子串的長度,最少添加的次數就是串長減子串長
            我用第二種方法做的
             
             1#include<stdio.h>
             2#include<string.h>
             3#include<math.h>
             4#define MAX 5005
             5int len,i,j;
             6char s[MAX],s1[MAX];
             7short f[MAX][MAX];
             8int max(int a,int b)
             9{
            10    if (a>b) return a; else return b;
            11}

            12int main()
            13{
            14    scanf("%d",&len);
            15    scanf("%s",&s);
            16    for (i=len-1;i>=0 ;i-- )
            17    {
            18        s[i+1]=s[i];
            19    }

            20    for (i=1;i<=len ;i++ )
            21    {
            22        s1[len-i+1]=s[i];
            23    }

            24    f[0][0]=0;
            25    for (i=1;i<=len ;i++ )
            26    {
            27        for (j=1;j<=len ;j++ )
            28        {
            29            if (s[i]==s1[j])
            30            {
            31                f[i][j]=max(f[i-1][j-1]+1,f[i][j]);
            32            }

            33            f[i][j]=max(f[i][j],f[i-1][j]);
            34            f[i][j]=max(f[i][j],f[i][j-1]);
            35        }

            36    }

            37    printf("%d\n",len-f[len][len]);
            38    return 0;
            39}

            40//當前狀態僅與上一層狀態有關,可以用滾動數組優化
            41

            我寫的比較惡心,沒用的東西太多
            Orz大神
             

            posted on 2012-02-20 18:34 jh818012 閱讀(338) 評論(0)  編輯 收藏 引用

            <2025年7月>
            293012345
            6789101112
            13141516171819
            20212223242526
            272829303112
            3456789

            導航

            統計

            常用鏈接

            留言簿

            文章檔案(85)

            搜索

            最新評論

            • 1.?re: poj1426
            • 我嚓,,輝哥,,居然搜到你的題解了
            • --season
            • 2.?re: poj3083
            • @王私江
              (8+i)&3 相當于是 取余3的意思 因為 3 的 二進制是 000011 和(8+i)
            • --游客
            • 3.?re: poj3414[未登錄]
            • @王私江
              0ms
            • --jh818012
            • 4.?re: poj3414
            • 200+行,跑了多少ms呢?我的130+行哦,你菜啦,哈哈。
            • --王私江
            • 5.?re: poj1426
            • 評論內容較長,點擊標題查看
            • --王私江
            久久亚洲国产成人精品性色| 国产欧美久久一区二区| 亚洲一级Av无码毛片久久精品| 久久久久婷婷| 999久久久无码国产精品| 狠狠精品干练久久久无码中文字幕 | 久久久久久A亚洲欧洲AV冫| 久久综合久久美利坚合众国| …久久精品99久久香蕉国产| 欧美国产精品久久高清| 精品熟女少妇av免费久久| 欧美精品丝袜久久久中文字幕| 久久亚洲春色中文字幕久久久| 久久最新免费视频| 久久香蕉国产线看观看99| 久久久亚洲欧洲日产国码aⅴ| 久久青青国产| 国内精品伊人久久久久网站| 久久er99热精品一区二区| 7777精品伊人久久久大香线蕉 | 日本精品久久久久影院日本| 久久er国产精品免费观看2| 久久棈精品久久久久久噜噜| 伊人色综合久久天天网| 精品无码人妻久久久久久| 热久久这里只有精品| 丁香五月网久久综合| 久久99久久99精品免视看动漫| 久久人妻无码中文字幕| 久久青青草视频| 综合网日日天干夜夜久久 | 成人精品一区二区久久 | 久久亚洲国产最新网站| 无码人妻少妇久久中文字幕| 久久影院午夜理论片无码| 久久精品国产WWW456C0M| 久久e热在这里只有国产中文精品99 | 五月丁香综合激情六月久久| 亚洲欧美久久久久9999 | 欧美久久综合九色综合| 亚洲精品成人久久久|