50136. Build Strings
難度:4.9/5
卡了70分鐘才想出來... 用global記次。
Second Try: 4.5/5 Used Time: 19:20 Remember to initialize all elements of string to '\0'。 Used Time: 10:341
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <string.h>
#include <stdint.h>
char c;
int k, l, n;
int counter = 0;
void gen_string(char s[], int now_len){
if(counter >= n) return;
if(now_len == l){
if(counter < n){
printf("%s\n", s);
counter++;
}
return;
}
else{
char prev = s[now_len - 1];
if(prev + k >= 'z'){
for(char now = 'a'; now <= prev + k - 26; now++){
s[now_len] = now;
gen_string(s, now_len + 1); if(counter >= n) return;
s[now_len] = 0;
}
for(char now = prev + 1; now <= 'z'; now++){
s[now_len] = now;
gen_string(s, now_len + 1); if(counter >= n) return;
s[now_len] = 0;
}
}
else{
for(char now = prev + 1; now <= prev + k; now++){
s[now_len] = now;
gen_string(s, now_len + 1); if(counter >= n) return;
s[now_len] = 0;
}
}
}
}
int main(){
scanf("%c %d %d %d", &c, &k, &l, &n);
char s[1050] = {0};
s[0] = c, s[l] = '\0';
gen_string(s, 1);
}