SRM474 Div2 Easy(250) PalindromesCount

PalindromesCount

#include <string>

using namespace std;

class PalindromesCount
{
    string reverse( string w );
public:
    int count( string A, string B );
};

string PalindromesCount::reverse( string w )
{
    string r;
    for ( int i=w.length()-1; i>=0; i-- )
        r += w[i];
    return r;
}

int PalindromesCount::count( string A, string B )
{
    int n = (int)A.length();
    int c = 0;

    for ( int i=0; i<=n; i++ )
    {
        string w = A.substr(0,i) + B + A.substr(i,n-i);
        if ( w == reverse(w) )
            c++;
    }

    return c;
}

追記
c++にreverse無いよなぁと思ってゴリゴリ書いていたが、rbegin(), rend()使えばいいのか。なるほど。

SRM 474 :: DIV2 - ichirin2501の日記

#include <string>

using namespace std;

class PalindromesCount
{
public:
    int count( string A, string B );
};

int PalindromesCount::count( string A, string B )
{
    int n = (int)A.length();
    int c = 0;

    for ( int i=0; i<=n; i++ )
    {
        string w = A.substr(0,i) + B + A.substr(i,n-i);
        if ( w == string(w.rbegin(),w.rend()) )
            c++;
    }

    return c;
}