dfs+search
1.数的划分
点击查看搜索
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,a[100];
void dfs(int x,int y,int z)//shu zong wei
{
if(y>n)return;
if(y==n)
{
for(int i=1;i<z;i++)//不取等
{
printf("%d ",a[i]);
}
printf("\n");
return;
}
for(int i=x;i<n;i++)
{
a[z]=i;
dfs(i,y+i,z+1);
}
}
int main()
{
scanf("%d",&n);
dfs(1,0,1);
return 0;
}
点击查看递推
/*把n看成是n个小球,k看成是k个盒子
那么题目就变成了把n小球放到k个盒子,且每个盒子都至少有1个小球的问题。
那么把n个小球放到k个盒子里的情况总数 = 1.至少有1个盒子放1个小球的情况总数 + 2.每个盒子都有多于1个小球的情况总数
那么这里 1 相当于 把n-1个小球放到k-1个盒子里的情况总数
2 相当于 把n-k个小球放到k个盒子里的情况总数
到了这里就可以发现是递推咯。
公式为 f(n,k) = f(n-1,k-1) + f(n-k,k)
*/
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int n, k, f[205][10];
int main() {
scanf("%d%d", &n, &k);//不太一样,划分成k个数
for (int i = 1; i <= n; i++) {
f[i][1] = 1;
}
for (int i = 2; i <= k; i++) {
f[i][i] = 1;
for (int j = i + 1; j <= n; j++) {
f[j][i] += f[j - 1][i - 1] + f[j - i][i];
}
}
printf("%d", f[n][k]);
return 0;
}
2.组合数的输出
点击查看代码
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int n,m,a[100];
bool b[100];
void print()
{
for(int i=1;i<=m;i++)printf("%d ",a[i]);
printf("\n");
return;
}
int dfs(int x,int y)
{
if(y>m) print();
else
{
for(int i=x;i<=n;i++)
{
a[y]=i;
dfs(i+1,y+1);
}
}
}
int main()
{
scanf("%d%d",&n,&m);
dfs(1,1);
return 0;
}
3.拔河比赛
详见Ybtoj
当然重点在于搜索都配有板子了
点击查看1
int search(int k)
{
for(int i=1;i<=n;i++)
{
if()//合法
{
//保存
if()//终点
{
ans++;
print();
}
else search(++k);
}
//恢复(回溯)
}
}
点击查看2
int search(int k)
{
if()//
{
ans++;
print();
}
else for(int i=1;i<=n;i++)
{
if()
{
//
search(++k);
//
}
}
}
注意场宽 && STL大法好
题目描述
第一行:n
第二行:n个字符(有重复)
输出:全排列+方案数
思路:把单一的 vis[i] 换为累加器数组
STL大法好
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<string.h>
#include<algorithm>
int n;
char s[30];
int main()
{
long long ans = 0;
scanf("%d",&n);
scanf("%s",s+1);
std::sort(s+1,s+n+1);
do{
for(int i=1;i<=n;i++) printf("%c",s[i]);
printf("\n");
++ans;
}while(std::next_permutation(s+1,s+n+1));
printf("%lld\n", ans);
return 0;
}
正解
#include <iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string.h>
using namespace std;
int n , ans = 0;
char s[100];
bool same( int t, int k) {
for(int i = t; i < k; ++i) {
if(s[k] == s[i]){
return 0;
}
}
return 1;
}
void print()
{
for(int i = 0; i < n; i++) {
printf("%c",s[i]);
}
printf("\n");
}
void dfs(int t) {
if(t == n-1) {
print();
ans++;
}
for(int i = t; i < n; ++i){
if(same(t, i)) {
swap(s[i], s[t]);
dfs( t + 1 );
swap(s[i], s[t]);
}
}
}
int main() {
scanf("%d",&n);
cin >> s;
dfs( 0);
printf("%d",ans);
return 0;
}
//数组尽量开大一点
dfs+search的更多相关文章
- 1923. Scary Politics (timus) (dfs) search
http://acm.timus.ru/problem.aspx?space=1&num=1923 -- timus This is s problem about thd dfs and s ...
- Principle of Computing (Python)学习笔记(7) DFS Search + Tic Tac Toe use MiniMax Stratedy
1. Trees Tree is a recursive structure. 1.1 math nodes https://class.coursera.org/principlescomputin ...
- [LeetCode#212]Word Search II
Problem: Given a 2D board and a list of words from the dictionary, find all words in the board. Each ...
- Leetcode: Android Unlock Patterns
Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...
- PE刷题记录
PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...
- Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- Trie for string LeetCode
Trie build and search class TrieNode { public: TrieNode * next[]; bool is_word; TrieNode(bool b = fa ...
- 582. Kill Process
Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...
- Leetcode: Closest Leaf in a Binary Tree
Given a binary tree where every node has a unique value, and a target key k, find the value of the n ...
随机推荐
- JMeter_响应数据为空以及中文乱码
一.响应数据为空 最近做测试接口,使用同样的请求方式.地址.参数和header,在postman中能正常响应,接收数据的也正常,但是在Jmeter中,虽然响应正常,但是响应数据却为空! Jmeter接 ...
- react中Fragment组件
什么是Fragment?在我们定义组件的时候return里最外层包裹的div往往不想渲染到页面,那么就要用到我们的Fragment组件了,具体使用如下: import React, { Compone ...
- Hive分区表和桶表的使用
原文链接: https://www.toutiao.com/i6766897068138037763/?group_id=6766897068138037763 我们看官网文档中这个地方 我们先创建好 ...
- XCTF(Web_php_unserialize)
拿到题目,是个这, 我们来一波代码审计 1 <?php 2 class Demo { 3 private $file = 'index.php'; 4 public function __con ...
- Flowable实战(四)BPMN2.0 启动与结束事件
一.BPMN2.0 BPMN2.0规范是一个标准,开源框架和不同供应商都遵循这份标准,使得最终用户不会因为依赖专有解决方案,而被供应商"绑架".有了BPMN2.0标准,不同解决 ...
- echart实现实时疫情图
直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> </h ...
- 【reverse】逆向2 寄存器与内存
[reverse]逆向2 寄存器与内存 1.通用寄存器 主要用途其实没必要记下来,因为只是CPU建议你这么做. 寄存器需要按照顺序被下来 32位就是可以存32个0或1 所以存储范围就是0-0xFFFF ...
- grafana中如何展示prometheus的延迟分布数据?
最终效果 最终在grafana中展示了一个服务每分钟的延迟分布: 各个部分的含义如下: 1.时间的分布,从0.01秒到最多720秒 2.用颜色深浅代表次数.颜色越深,请求次数越多 3.时间轴,代表在这 ...
- mysql主从模型下如果保证主误删除数据,尽可能避免数据丢失方案
- 开发者的瑞士军刀「GitHub 热点速览 v.22.04」
Swiss Army knife 可以说是本周的关键词了,多个项目采用该词来描述它的特性:像是能全方位解决浏览器"网络"操作的 CyberChef 方便你进行数据加密.解编码,还有 ...