Joke with permutation 搜索

                      Joke with permutation

题目抽象:对于1~n的排列,用空格将整数隔开。现在将空格哦去掉,成了一个没有空格的数字字符串。请将他还原,如有多个,输出任意一个即可。

分析:首先根据长度可以出去 n . 然后搜索。给DFS字符串位置参数。  对于每个位置的字符cur,可以自己组成一个整数,也可以和下一位组成一个整数。

没组成一个整数x,就将flag[x] = 1. 注意0只能在两位数的整数的各位。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <string>
 7 #include <vector>
 8 #include <set>
 9 #include <map>
10 #include <queue>
11 #include <stack>
12 using namespace std;
13 typedef long long LL;
14 const LL INF = 0x4fffffff;
15 const double EXP= 1e-5;
16 const LL MOD = 1e9+7;
17 const int MS= 105;
18
19 bool flag[MS];
20 bool tag[MS];
21 char str[MS];
22 bool over;
23 int len,n;
24
25 void dfs(int cur)
26 {
27     if(over)
28         return ;
29     if(cur >= len)
30     {
31         for(int i = 0 ;i < len; i++)
32         {
33                 printf("%c",str[i]);
34                 if(tag[i] == 1 && i != len -1 )
35                         printf(" ");
36         }
37         over = true;
38         return ;
39     }
40
41     //  0只能在两位数的整数中的个位。这里保证下一次dfs中的cur位置不会是0.
42
43     if(!flag[str[cur] - ‘0‘] && ( cur + 1 >= len || str[cur + 1] != ‘0‘))
44     {
45                 tag[cur] = 1;    // 这里表示在这个位置组成一个整数,便于确定空格的位置。
46                 flag[str[cur] - ‘0‘ ] = 1;
47                 dfs(cur + 1);
48                 tag[cur] = 0;                         //回退。
49                 flag[str[cur] - ‘0‘]  = 0;
50     }
51
52     if(!flag[(str[cur] - ‘0‘) * 10 + str[cur + 1] - ‘0‘] && ((str[cur] - ‘0‘) * 10 + str[cur+1] -‘0‘) <= n && (cur + 2 >= len || str[cur + 2] != ‘0‘))
53     {
54                 tag[cur + 1] = 1;
55                 flag[(str[cur] - ‘0‘) * 10 + str[cur + 1] - ‘0‘] = 1;
56                 dfs(cur + 2);
57                 tag[cur + 1 ] = 0;
58                 flag[(str[cur] - ‘0‘) * 10 + str[cur + 1] - ‘0‘] = 0;
59     }
60 }
61
62
63 int main()
64 {
65     scanf("%s",str);
66     len = strlen(str);
67     if(len <= 9)
68     {
69         for(int i = 0; i < len; i++)
70         {
71                 if(i == 0)
72                     printf("%c",str[i]);
73                 else
74                     printf(" %c",str[i]);
75         }
76         return 0;
77     }
78     n = (len - 9) / 2 + 9;
79     over = false;
80     memset(flag,0,sizeof(flag));
81     memset(tag,0,sizeof(tag));
82     dfs(0);
83     return 0;
84 }
时间: 2024-07-28 12:44:45

Joke with permutation 搜索的相关文章

HNU Joke with permutation (深搜dfs)

题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=13341&courseid=0 Joke with permutation Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:65536KB Total submit users: 85, Accepted users: 57 Problem 13341 : Special judge Prob

【搜索】【并查集】Codeforces 691D Swaps in Permutation

题目链接: http://codeforces.com/problemset/problem/691/D 题目大意: 给一个1到N的排列,M个操作(1<=N,M<=106),每个操作可以交换X Y位置上的数字,求可以得到的最大字典序的数列. 题目思路: [搜索][并查集] 这题可以用搜索或者并查集写,都能过. 把位置分成若干块,每一块里面的位置都是可以被这一块里另一个位置经过若干次调换的(类似强连通,位置可达). 然后把每一块位置里的 位置按从小到大排序,位置上的值按从大到小排序,依次填入位置

HDU 6044--Limited Permutation(搜索+组合数+逆元)

题目链接 Problem Description As to a permutation p1,p2,?,pn from 1 to n, it is uncomplicated for each 1≤i≤n to calculate (li,ri) meeting the condition that min(pL,pL+1,?,pR)=pi if and only if li≤L≤i≤R≤ri for each 1≤L≤R≤n. Given the positive integers n, (

深度优先搜索思想初体验

1.求数字 1~n 的全排列 import java.util.Scanner ; public class Permutation{ //求数字 1~n 的全排列: int[] array ; int[] book ; int n ; public void permutation(int step){ // 深度优先搜索思想: if (step==n+1) { for (int i=1;i<=n;i++) { System.out.print(array[i] + " ")

soj4271 Love Me, Love My Permutation (DFS)

4271: Love Me, Love My Permutation Description Given a permutation of n: a[0], a[1] ... a[n-1], ( its elements range from 0 to n-1, For example: n=4, one of the permutation is 2310) we define the swap operation as: choose two number i, j ( 0 <= i <

PHP和MySQL Web开发从新手到高手,第6天-创建joke管理页面

1. 创建joke管理页面   主要包含以下几个页面:   A. index.php, 准备各种变量数据.供展示页面使用.   B. jokes.html.php, 显示joke.   C. form.html.php, 用于编缉或添加笑话的页面.   D. search.form.html.php, 用于搜索笑话的面面. 页面郊果:   2. index.php的主要流程 2.1 是否已登录 if (!user_is_login()){ include '../login.html.php';

Codeforces 509F Progress Monitoring (区间dp 或 记忆化搜索)

F. Progress Monitoring time limit per test 1 second memory limit per test 256 megabytes Programming teacher Dmitry Olegovich is going to propose the following task for one of his tests for students: You are given a tree T with n vertices, specified b

[LeetCode] 031. Next Permutation (Medium) (C++/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 031. Next Permutation (Medium) 链接: 题目:https://oj.leetcode.com/problems/next-permutation/ 代码(github):https://github.com/illuz/leetcode 题意: 求一个序列的下一个排列. 分析: 可以用

数据结构基础(3) --Permutation &amp; 插入排序

Permutation(排列组合) 排列问题: 设R = {r1, r2, ... , rn}是要进行排列的n个元素, Ri = R-{ri}; 集合X中元素的全排列记为Permutation(X), (ri)Permutation(X)表示在全排列Permutation(X)的每一个排列前加上前缀ri得到的排列. R的全排列可归纳定义如下: 当n=1时,Permutation(R)={r},r是集合R中唯一的元素: 当n>1时,Permutation(R)由(r1)Permutation(R1