Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

怎么能比较高效的搜呢,我在本机5000都跑不出来。。。

Posted by Debugcool at 2010-03-24 15:59:18 on Problem 2558
rt
code
--------------------------------------------------------------------------
#include <iostream>
#include <string>
#define LEN 5120
using namespace std;
const char key[] = "NOP";
int N;
char ans[LEN];
bool isHard()
{
    string output(ans);
    int len = output.length();
    if (len == 1)
        return true;
    for (int l = 1;l <= len / 2;l++)
        for (int i = 0;i < len - l;i++)
            if (output.substr(i,l) == output.substr(i + l,l))
                return false;
    return true;
}
bool DFS(int len,char pre)
{
    if (!isHard())
        return false;
    if (len == N)
        return true;
    for (int i = 0;i < 3;i++)
    {
        if (key[i] == pre)
            continue;
        ans[len] = key[i];
        ans[len+1] = '\0';
        if (DFS(len + 1,key[i]))
            return true;
    }
    return false;
}
int main()
{
    N = 5000;
    for (int i = 0;i < 3;i++)
    {
        ans[0] = key[i];
        ans[1] = '\0';
        if (DFS(1,key[i]))
            break;
    }
    while (scanf("%d",&N), N)
    {
        for (int i = 0;i < N;i++)
            printf("%c",ans[i]);
        printf("\n");
    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator