Panasonic Programming Contest 2020 D - String Equivalence ###K ###K //K

题目链接:https://atcoder.jp/contests/panasonic2020/tasks/panasonic2020_d

题意:给定n 输出所有长度为n的 要求字符串, 要求的字符串是满足 所有 s[i]=s[j]时t[i]=t[j]  或者 s[i]!=s[j]时 t[i]!=t[j]时的最小的那个字符串

思路:这种直接for 不出来的 不是递归搜 就是状压枚举  这里的是 dfs 搜索 用string  来记录上一个是什么

刚开始第一个只能是a  有了a就可以有下一个b  以此类推 当 前面已经出现过这个字符时,后面一定可以使用

复杂度一定小于 n的阶乘   因为是这其中的子集

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define ull unsigned long long
 5 #define pb push_back
 6 const int maxn=2e5+10;
 7 const int mod=998244353;
 8 
 9 int n;
10 int f[15];
11 
12 void dfs(int cnt,string s)
13 {
14     if(cnt>n)
15     {
16         cout<<s<<'\n';
17         return;
18     }
19     for(int i=1;i<=cnt;i++)
20     {
21         string ss=s;
22         if(f[i]==0)
23             continue;
24         ss+=char('a'+i-1);
25         f[i+1]++;
26         dfs(cnt+1,ss);
27         f[i+1]--;
28     }
29 }
30 
31 int main()
32 {
33     ios::sync_with_stdio(false);
34     cin.tie(0);
35     cin>>n;
36     string a;
37     f[1]=1;
38     dfs(1,a);
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 }
View Code

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define ll long long
 4 #define pb push_back
 5 const int maxn =1e5+10;
 6 const int mod=1e9+7;
 7 
 8 int n;
 9 
10 void dfs(string s,int num)
11 {
12     if(s.size()==n)
13     {
14         cout<<s<<'\n';
15         return;
16     }
17     for(int i=0;i<=num;i++)
18     {
19         char d=('a'+i);
20         string k=s;
21         k+=d;
22         if(i!=num)
23             dfs(k,num);
24         else
25             dfs(k,num+1);
26     }
27 
28 }
29 
30 int main()
31 {
32     ios::sync_with_stdio(false);
33     cin.tie(0);
34     cin>>n;
35     string s;
36     dfs(s,0);
37 
38 
39 
40 
41 }
View Code

 

posted @ 2020-07-12 21:26  canwinfor  阅读(146)  评论(0)    收藏  举报