当前位置:网站首页>[depth first search] Ji suanke: a joke of replacement
[depth first search] Ji suanke: a joke of replacement
2022-07-06 18:46:00 【muse_ age】
The question :
Given a string , Find the separation scheme , Separate the string into non repeating numbers
Ideas :
Because the string length is less than 100, therefore n yes 2 digit .
You can separate them in groups , You can also work in groups of two
( It can be seen as walking through a one-dimensional maze , from S[0] Go to the S[n-1], One step at a time , Or take two steps .)
DFS(int index)// The subscript
prune :
(1) Feasibility pruning ( Transboundary ):if(index>=n)return;
(2) Repetitive pruning (vis Array ): If there have been numbers , There is no need to search
(3) Optimal pruning ( With global variables flag): If there is already a set of solutions , Then there is no need to search
To make a choice :
Mark the number
Digital join vector Array
DFS(index+1)// Take a step
Recall mark
vector.pop_back();
Mark the number
Digital join vector Array
DFS(index+2)// Take two steps
Recall mark
vector.pop_back();
#include<iostream>
#include<string>
#include<vector>
using namespace std;
bool vis[100];
vector<int>num;
string s;
int n;
bool flag=false;
void dfs(int step){
if(flag){
return;
}
if(step>=s.size()){
for(int i=0;i<num.size();i++){
cout<<num[i]<<" ";
}
flag=true;
return;
}
if(!vis[s[step]-'0']){
vis[s[step]-'0']=true;
num.push_back(s[step]-'0');
dfs(step+1);
vis[s[step]-'0']=false;
num.pop_back();
}
if((s[step]-'0')!=0&&!vis[(s[step]-'0')*10+s[step+1]-'0'])
{
vis[(s[step]-'0')*10+s[step+1]-'0']=true;
num.push_back((s[step]-'0')*10+s[step+1]-'0');
dfs(step+2);
vis[(s[step]-'0')*10+s[step+1]-'0']=false;
num.push_back((s[step]-'0')*10+s[step+1]-'0');
}
}
int main(){
cin>>s;
dfs(0);
}
边栏推荐
- 根据PPG估算血压利用频谱谱-时间深度神经网络【翻】
- If you have any problems, you can contact me. A rookie ~
- 美庐生物IPO被终止:年营收3.85亿 陈林为实控人
- Introduction to the use of SAP Fiori application index tool and SAP Fiori tools
- 2022/02/12
- 44所高校入选!分布式智能计算项目名单公示
- Docker installation redis
- 首先看K一个难看的数字
- 44 colleges and universities were selected! Publicity of distributed intelligent computing project list
- DOM Brief
猜你喜欢
Introduction and case analysis of Prophet model
[the 300th weekly match of leetcode]
If you have any problems, you can contact me. A rookie ~
Medical image segmentation
十、进程管理
287. 寻找重复数
This article discusses the memory layout of objects in the JVM, as well as the principle and application of memory alignment and compression pointer
helm部署etcd集群
重磅硬核 | 一文聊透对象在 JVM 中的内存布局,以及内存对齐和压缩指针的原理及应用
2022-2024年CIFAR Azrieli全球学者名单公布,18位青年学者加入6个研究项目
随机推荐
Method of accessing mobile phone storage location permission under non root condition
视频化全链路智能上云?一文详解什么是阿里云视频云「智能媒体生产」
DOM简要
2022/02/12
Some understandings of tree LSTM and DGL code implementation
徐翔妻子应莹回应“股评”:自己写的!
Solve DoS attack production cases
解读云原生技术
Markdown syntax for document editing (typera)
wx小程序学习笔记day01
抽象类与抽象方法
Deep circulation network long-term blood pressure prediction [translation]
Describe the process of key exchange
Stm32+esp8266+mqtt protocol connects onenet IOT platform
Visual Studio Code启动时提示“Code安装似乎损坏。请重新安装。”、标题栏显示“不受支持”信息的解决办法
Installation and management procedures
With the implementation of MapReduce job de emphasis, a variety of output folders
测试行业的小伙伴,有问题可以找我哈。菜鸟一枚~
STM32+ENC28J60+UIP协议栈实现WEB服务器示例
测试1234