题目:Krypton Factor
题意:困难的串只没有量个相同的子串相邻。找出第n个由前L个字母构成的困难的串。
思路:dfs。注意输出格式。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
using namespace std;int n,m;
vector<int> vec;
vector<string> a;bool judge() {for(int i=1; i<=vec.size()/2; i++) {for(int j=1; j<=i; j++) {if(vec[vec.size()-i+j-1]!=vec[vec.size()-2*i+j-1]) goto END;}return false;END:;}return true;
}bool dfs(int x) {string y;for(int i=0; i<vec.size(); i++) {y+=(vec[i]-1+'A');}if(judge()) {a.push_back(y);if(a.size()==n+1) return true;} else {return false;}for(int i=1; i<=m; i++) {vec.push_back(i);if(dfs(x+1)) return true;vec.pop_back();}return false;
}int main() {while(scanf("%d%d",&n,&m)==2&&n!=0&&m!=0) {vec.clear(),a.clear();dfs(1);string x=a[n];for(int i=0;i<x.size();i++){if(i!=0&&i%64==0) cout<<endl;else if(i!=0&&i%4==0) cout<<' ';cout<<x[i];}cout<<endl;cout<<x.size()<<endl;}return 0;
}