SGU 510 Distinct Substrings(IDA*)

题意:给定数字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();
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值