码迷,mamicode.com
首页 > 其他好文 > 详细

zoj 2744 - Palindromes

时间:2014-10-02 00:27:01      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:blog   io   os   ar   for   sp   2014   c   log   

题目:统计一个串中的回文子串的个数(注意是子串,要连续)。

分析:dp,暴力。直接用dp,二维数组内存不够用,而且dp木有暴力快( ⊙ o ⊙ )啊!

说明:(2011-09-24 03:22)。

#include <iostream>
#include <cstdlib> 
#include <cstring>

using namespace std;

char data[ 5005 ];
bool F[ 5005 ][ 5005 ];

int main()
{
    while ( cin >> data ) {
        int Len = strlen( data );
        memset( F, false, sizeof( F ) );
        for ( int i = 0 ; i < Len ; ++ i )
            F[ i ][ i ] = true;
        for ( int i = Len-1 ; i >= 0 ; -- i )
        for ( int j = i+1 ; j < Len ; ++ j ) 
            if ( data[ i ] == data[ j ] && ( i+1 >= j-1 || F[ i+1 ][ j-1 ] ) )
                F[ i ][ j ] = true;
        
        int count = 0;
        for ( int i = 0 ; i < Len ; ++ i )
        for ( int j = i ; j < Len ; ++ j )
            if ( F[ i ][ j ] ) ++ count;
        
        cout << count << endl;
    }
    return 0;
}

zoj 2744 - Palindromes

标签:blog   io   os   ar   for   sp   2014   c   log   

原文地址:http://blog.csdn.net/mobius_strip/article/details/39719119

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!