zoukankan      html  css  js  c++  java
  • [POJ] Palindrome

    Palindrome
    Time Limit: 3000MS   Memory Limit: 65536K
    Total Submissions: 62102   Accepted: 21643

    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

    本题是一个求LCS长度的问题,具体算法及思路可以参考

    http://blog.csdn.net/v_july_v/article/details/6695482

    注意用int会爆空间

    #include<iostream>
    using namespace std;
    
    short c[5005][5005];
    char s[5005];
    
    int main()
    {
    	int n;
    	cin>>n;
    
    	for(int i=1;i<=n;i++)
    	{
    		cin>>s[i];
    	}
        
    	for(int i=0;i<=n;i++)
    		c[i][0]=c[0][i]=0;
    
        for(int i=1;i<=n;i++)
        	for(int j=1;j<=n;j++)
        	{
        		if(s[i]==s[n-j+1])
        			c[i][j]=c[i-1][j-1]+1;
        		else if(c[i-1][j]>=c[i][j-1])
        			c[i][j]=c[i-1][j];
        		else
        			c[i][j]=c[i][j-1];
        	}
    
        cout<<n-c[n][n]<<endl;
    
        return 0;
    }
    

      

  • 相关阅读:
    Intent
    关注博客
    Bitmap
    图片压缩
    读取相册、拍照
    Godot开发环境与学习资源
    源码开放的引擎研究
    海龟交易法操作商品期货
    重新开始
    使用node_redis进行redis数据库crud操作
  • 原文地址:https://www.cnblogs.com/KennyRom/p/6486934.html
Copyright © 2011-2022 走看看