Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
怎么能比较高效的搜呢,我在本机5000都跑不出来。。。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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator