题意:给定数字n,表示串s所有不同串的个数,要求s最短并且字典序最小
题解:刚开始找规律,emm找的有点费劲,普通dfs 就TLE,所以就是IDA*啦
给定上界limit,当前深度cur,k = limit - cur,当前串个数temp
如果temp + cur * k + (1 + k) * k / 2 < n,则直接剪枝
即,后面每个都加上不同字母所能形成的最多的串的个数都不够n
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
#define MOD 1000000007
#define maxn 1e6 + 5
using namespace std;
int n;
set < string > ss;
bool dfs(string s, int cur, int limit){
if(cur > limit) return false;
ss.clear();
int len = (int)s.length();
for(int i=0; i<len; i++){
for(int j=1; j+i<=len; j++){
ss.insert(s.substr(i,j));
}
}
if(ss.size() == n){
cout << s << endl;
return true;
}
else{
int k = limit - cur;
if(ss.size() + cur * k + (1 + k) * k / 2 < n)
return false;
}
for(int i=0; i<25; i++){
char ch = 'a' + i;
if(dfs(s+ch, cur+1, limit))
return true;
}
return false;
}
void solve(){
for(int i=1; i<=25; i++){
if(dfs("",0,i))
return;
}
return;
}
int main(){
//freopen("distinct.in","r",stdin);
//freopen("distinct.out","w",stdout);
cin >> n;
solve();
}