yt99 题解分享 · 2024/4/8
外星密码 - 题解
``` #include<bits/stdc++.h> using namespace std; string expand(){ string s="",X; char c;int D; while(cin>>c){ if(c=='['){ cin>>D; //输入完数字后,则到 ] 都要输入,用X来存储 X = expand(); while(D--) s +=X;//数字是几,则扩大几倍 } else if(c==']') return s; //结束条件 else s+=c; //存储数字后的字符 返回后,则存于X里 } return s; } int main(){ cout<<expand()<<endl; return 0; } ```
查看全文
0 0 2 1
Heng_Xin 题解分享 · 2024/4/5
外星密码 - 题解
```C++ #include <iostream> #include <string> #include <stack> #include <functional> using namespace std; int main() { function<string()> dfs = [&]() { int n; string str = "", tmp; char x; while (cin >> x) { if (x == '[') { cin >> n; tmp = dfs(); // 递归 while (n--) str += tmp; } else if (x == ']') { return str; } else { str += x; } } return str; }; cout << dfs() << "\n"; return 0; } ```
查看全文
0 0 2 0
seabra 题解分享 · 2024/4/9
外星密码 - 题解
include using namespace std; string def(){ string s="";//记录本层的字符; int d;//记录下一层返回时扩展的倍数。 char c;//中间输入的变量。 //这个题目两个条件: //1.找到结束条件。2.何时调用自己。 while(cin>>c){ if(c=='['){ cin>>d; string x = def(); for(int i=1;i<=d;i++) s+=x; } else if(c==']'){ return s; }else{ s+=c; } } return s; } int main(){ cout<<def()<<endl; //getchar(); return 0; }
查看全文
0 0 1 0
Well 题解分享 · 2024/5/22
外星密码 - 题解
``` def decode_string(s): def decode_helper(index): decoded_string = '' num = 0 while index < len(s): char = s[index] if char.isdigit(): num = num * 10 + int(char) elif char == '[': index, decoded_part = decode_helper(index + 1) decoded_string += decoded_part # num = 0 elif char == ']': if num: decoded_string = num * decoded_string return index, decoded_string else: decoded_string += char index += 1 return decoded_string return decode_helper(0) # 输入的压缩字符串 compressed_string = input() # 解压缩后的字符串 decoded_string = decode_string(compressed_string) print(decoded_string) ```
查看全文
0 0 0 1
slz201806 题解分享 · 2024/4/12
外星密码 - 题解
``` 用的栈模拟的 code = input() n = len(code) p=0 stake=[] ret=[] t="0" def decode(tp): i=tp[0] i=int(i) j=tp[1:] all = i* j all="".join(all) return all while p < n: if(code[p]!="]"): stake.append(code[p]) p+=1 elif(code[p]=="]"): temp = [] while(stake[-1]!="["): t=stake.pop() temp.append(t) stake.pop() temp.reverse() dc = decode(temp) stake.append(dc) p+=1 ret_final=''.join(stake) print(ret_final) ```
查看全文
0 0 0 0
挑战10天蓝桥杯省一 题解分享 · 2024/4/5
外星密码 - 题解
分享给各位网上的题解(Java) ~ import java.util.; public class 外星密码 { static int index = 0; public static void main(String[] args) { Scanner scan = new Scanner(System.in); char s [] = scan.next().toCharArray(); scan.close(); System.out.println(dfs(s)); } public static String dfs(char [] s){ String ss = "",s1; while(index =0&&x 0) { ss +=s1; } }else { if(s[index] == ']') { index++; return ss; } else { ss +=s[index]; index ++; } } } return ss; } } ~
查看全文
0 0 0 0