SRM488 Div2 Medium(500) TheBoringStoreDivTwo

TheBoringStoreDivTwo

文字列長が短いのでO(n8)でも間に合う。

#include <string>
using namespace std;

class TheBoringStoreDivTwo{public:
string find( string J, string B )
{
    int x = (int)J.length();
    int y = (int)B.length();

    string ans = "";

    for ( int i=0;   i<x; i++ )
    for ( int j=i;   j<x; j++ )
    for ( int k=j+1; k<x; k++ )
    for ( int l=k;   l<x; l++ )
    for ( int m=0;   m<y; m++ )
    for ( int n=m;   n<y; n++ )
    for ( int o=n+1; o<y; o++ )
    {
        int p = ((j-i)+(n-m)) - ((l-k)+(-o));

        if ( o <= p  &&  p < y )
        {
            string n1 = J.substr(i,j-i+1) + B.substr(m,n-m+1);
            string n2 = J.substr(k,l-k+1) + B.substr(o,p-o+1);
            if ( n1 == n2 )
                if ( n1.length() > ans.length()  ||
                     n1.length() == ans.length()  &&  n1 < ans )
                    ans = n1;
        }

        p = ((l-k)+(n-m)) - ((j-i)+(-o));

        if ( o <= p  &&  p < y )
        {
            string n1 = J.substr(k,l-k+1) + B.substr(m,n-m+1);
            string n2 = J.substr(i,j-i+1) + B.substr(o,p-o+1);
            if ( n1 == n2 )
                if ( n1.length() > ans.length()  ||
                     n1.length() == ans.length()  &&  n1 < ans )
                    ans = n1;
        }
    }

    return ans;
}};