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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. [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 ...

  4. Leetcode: Android Unlock Patterns

    Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...

  5. PE刷题记录

    PE刷题记录 PE60 / 20%dif 这道题比较坑爹. 所有可以相连的素数可以构成一张图,建出这张图,在其中找它的大小为5的团.注意上界的估算,大概在1W以内.1W内有1229个素数,处理出这些素 ...

  6. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  7. Trie for string LeetCode

    Trie build and search class TrieNode { public: TrieNode * next[]; bool is_word; TrieNode(bool b = fa ...

  8. 582. Kill Process

    Problem statement: Given n processes, each process has a unique PID (process id) and its PPID (paren ...

  9. 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 ...

随机推荐

  1. Python + Selenium 定位非selected型下拉框的方法

    最近在尝试给自己负责的模块写UI自动化的Demo 登录及切换页面比较顺利 但是遇到下拉框的选择时,遇到了一点困难 我负责的模块页面的下拉框并非Select类型,无法使用select_by_index ...

  2. HDU分拆素数和

    https://acm.hdu.edu.cn/showproblem.php?pid=2098 时间复杂度 #include<bits/stdc++.h> using namespace ...

  3. HttpServer: 基于IOCP模型且集成Openssl的轻量级高性能web服务器

    2021年4月写过一个轻量级的web服务器HttpServer,见文章: <HttpServer:一款Windows平台下基于IOCP模型的高并发轻量级web服务器>,但一直没有时间添加O ...

  4. vue学习18-过滤器

    <!DOCTYPE html> <html lang='en'> <head> <meta charset='UTF-8'> <meta http ...

  5. mongo用户认证

    mongo@rayos:/opt/mongodb$ mongo --port 28017 MongoDB shell version v4.0.13 connecting to: mongodb:// ...

  6. python22day

    内容回顾 递归练习 sys os logging shutil 函数结束啦 今日内容 面向对象 楔子:做一个人狗大战的游戏 技能要有归属感,人是人,狗是狗,技能的函数要写在对应函数内部,闭包. 复杂的 ...

  7. Redis 源码简洁剖析 04 - Sorted Set 有序集合

    Sorted Set 是什么 Sorted Set 命令及实现方法 Sorted Set 数据结构 跳表(skiplist) 跳表节点的结构定义 跳表的定义 跳表节点查询 层数设置 跳表插入节点 zs ...

  8. JavaScript数据结构之链表

    链表相较于数组的优缺点 1. 链表在 插入.删除.移动数据效率比数组要高,数组插入.移动.删除数据需要改变没有数据的索引,而链表则只需要更改指针即可 2. 在查询方面,数组要优于链表,数组存储的数据是 ...

  9. 体验 正式发布 的OSM v1.0.0 版本

    2021年10月份发布了OSM 1.0 RC[1],在过去的几个月里,OSM 的贡献者一直在努力为 v1.0.0 版本的发布做准备.2022年2月1日,OSM 团队正式发布 1.0.0 版本[2]. ...

  10. SpringBoot Log4j 安全漏洞分析及解决方案

    一.序言 SpringBoot作为Java基础框架大行其道,前不久爆发出Log4j安全漏洞,大众更多关心Log4j的危害是多么严重,然而鲜有关心SpringBoot这一底层框架的安全性问题,换而言之, ...