当前位置:网站首页>[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);
}边栏推荐
- Stm32+esp8266+mqtt protocol connects onenet IOT platform
- STM32+ENC28J60+UIP协议栈实现WEB服务器示例
- Atcoder a mountaineer
- Nuc11 cheetah Canyon setting U disk startup
- [sword finger offer] 60 Points of N dice
- Afnetworking framework_ Upload file or image server
- 关于npm install 报错问题 error 1
- Numerical analysis: least squares and ridge regression (pytoch Implementation)
- 徐翔妻子应莹回应“股评”:自己写的!
- 解读云原生技术
猜你喜欢

The role of applet in industrial Internet

Shangsilicon Valley JUC high concurrency programming learning notes (3) multi thread lock

On AAE

Tree-LSTM的一些理解以及DGL代码实现

Solve DoS attack production cases

人体骨骼点检测:自顶向下(部分理论)

巨杉数据库首批入选金融信创解决方案!

同宇新材冲刺深交所:年营收9.47亿 张驰与苏世国为实控人
![A wearable arm device for night and sleeveless blood pressure measurement [translation]](/img/fd/947a38742ab1c4009ec6aa7405a573.png)
A wearable arm device for night and sleeveless blood pressure measurement [translation]
![Optical blood pressure estimation based on PPG and FFT neural network [translation]](/img/88/2345dac73248a5f0f9fa3142ca0397.png)
Optical blood pressure estimation based on PPG and FFT neural network [translation]
随机推荐
From 2022 to 2024, the list of cifar azrieli global scholars was announced, and 18 young scholars joined 6 research projects
With the implementation of MapReduce job de emphasis, a variety of output folders
10、 Process management
Brief description of SQL optimization problems
Deep circulation network long-term blood pressure prediction [translation]
Stm32+esp8266+mqtt protocol connects onenet IOT platform
Shangsilicon Valley JUC high concurrency programming learning notes (3) multi thread lock
There is a sound prompt when inserting a USB flash disk under win10 system, but the drive letter is not displayed
Penetration test information collection - App information
Grafana 9.0 is officially released! It's the strongest!
Interpreting cloud native technology
用友OA漏洞学习——NCFindWeb 目录遍历漏洞
Medical image segmentation
Automatic reservation of air tickets in C language
Execution process of MySQL query request - underlying principle
朗坤智慧冲刺科创板:年营收4亿 拟募资7亿
Penetration test information collection - CDN bypass
当保存参数使用结构体时必备的开发技巧方式
Picture zoom Center
Cobra quick start - designed for command line programs