Metal Steak

Hard to eat

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 79 Stories :: 0 Comments :: 0 Trackbacks

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论


#include 
<iostream>
using namespace std;

int
f[
2][5001], l, maxi;    //为了优化空间,,使用了滚动数组
string
str, strr;

void
__read__()
{
    cin 
>> l >> str;
}

void
__reverse__()
{
    
forint i = 1; i <= l; i++ )
        strr 
+= str[l - i];
}

void
__lcs__()
{
    
int
    cur 
= 1, old = 1;
    
forint i = 1; i <= l; i++ )
    {
        cur 
= i % 2;
        old 
= ( i - 1 ) % 2;
        
forint j = 1; j <= l; j++ )
            
if( str[i - 1== strr[j - 1] )
            {
                f[cur][j] 
= f[old][j - 1+ 1;
                
if( maxi < f[cur][j] )
                    maxi 
= f[cur][j];
            }
            
else
            {
                
if( f[old][j] > f[cur][j - 1] )
                    f[cur][j] 
= f[old][j];
                
else
                    f[cur][j] 
= f[cur][j - 1];
            }
    }
}

void
__outp__()
{
    cout 
<< l - maxi << endl;
}

int
main()
{
    __read__();
    __reverse__();
    __lcs__();
    __outp__();

    
return 0;
}

posted on 2009-09-15 20:58 mad4alcohol 阅读(110) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理