首页 > 其他分享 >Sequence Decoding (DFS)

Sequence Decoding (DFS)

时间:2023-02-03 11:07:04浏览次数:38  
标签:数字 Sequence int DFS Decoding char ++ dfs include


Sequence Decoding (DFS)_字符串

Sequence Decoding (DFS)_#include_02

Sequence Decoding (DFS)_字符串_03

给出一个字符串,只含 【,】,H,P,和数字,要把这些数字和中括号里的字母展开复制多少遍根据中括号外面的数字决定。

用深搜来做,遇到数字就记录数字多少,遇到【就进深一度的搜索,如果数H或者P的就把这些字符按照前面的数字给扩大几倍。

AC代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stdlib.h>
#include<queue>
#include<map>
#include<iomanip>
#include<math.h>
using namespace std;
typedef long long ll;
typedef double ld;
const int INF = 0x3f3f3f3f;
int i,j,k;
const int N = 1010;

char *dfs(char s[])
{
char *p;
while(1)
{
int k=0;
while(isdigit(*s))
{
k=k*10+(*s-'0');
s++;
}
if(k==0)
k++;
if(*s=='[')
{
for(int i=0; i<k; i++)
p=dfs(s+1);
s=p;
}
else if(*s=='P'||*s=='H')
{
for(int i=0;i<k;i++)
printf("%c",*s);
s++;
}
else
return s+1;
}
}

int n;
char s[N];

int main()
{
scanf("%d",&n);
while(n--)
{
scanf("%s",s);
dfs(s);
printf("\n");
}
return 0;
}

 

标签:数字,Sequence,int,DFS,Decoding,char,++,dfs,include
From: https://blog.51cto.com/u_15952369/6035566

相关文章

  • 【DFS】LeetCode 236. 二叉树的最近公共祖先
    题目链接236.二叉树的最近公共祖先思路代码classSolution{publicTreeNodelowestCommonAncestor(TreeNoderoot,TreeNodep,TreeNodeq){if(roo......
  • codeforces 580C Kefa and Park (树上DFS)
    Description:Kefadecidedtocelebratehisfirstbigsalarybygoingtotherestaurant.Helivesbyanunusualpark.Theparkisarootedtreeconsistingof n ve......
  • [LeetCode]Permutation Sequence
    QuestionTheset[1,2,3,…,n]containsatotalofn!uniquepermutations.Bylistingandlabelingallofthepermutationsinorder,Wegetthefollowingsequen......
  • HDU-1159-Common Subsequence
    ​​题目链接​​​题目大意:给出两个字符串,求两个字符串的最长公共字串。思路:慢慢重心开始有贪心转向动态规划了,这题就是简单的动态规划题。以题目的第一组测试数据为例......
  • 【题解】CF1770F Koxia and Sequence
    有没有觉得其他题解的模二Lucas逆用太智慧了,有没有觉得这题的第一思路是直接拆位算每一位是否有贡献,而不是先满足和的限制列式?这里提供另外一个做法。方向不同,结果一样......
  • poj-1458-Common Subsequence
    CommonSubsequenceTimeLimit:1000MSMemoryLimit:10000KTotalSubmissions:43207Accepted:17522DescriptionAsubsequenceofagivensequenceisthegivenseq......
  • 【DFS】LeetCode 124. 二叉树中的最大路径和
    题目链接124.二叉树中的最大路径和思路一个子树内部的最大路径和=左子树提供的最大路径和+根节点值+右子树提供的最大路径和。即dfs(root.left)+root.val+dfs(r......
  • hive的Caused by: org.apache.hadoop.hdfs.BlockMissingException: Could not obtain
    早上起来去跑个hive的sql,稍微复杂点sql,就会报错如Causedby:org.apache.hadoop.hdfs.BlockMissingException:Couldnotobtainblock:BP-572947236等,经过一个一个小时......
  • 言简意赅的 Kahn 和 DFS 拓扑排序 (C++实现)
    I邻接表(AdjacentList)实现有向图在类内定义结构体Vertex(顶点).Vertex包含三个成员:data表示某顶点内所存放的内容,类型由模板决定(默认char)indegree表......
  • LeetCode合并两个有序链表(/dfs)
    题目将两个升序链表合并为一个新的升序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。约束解法方法一classSolution{public:ListNode......