Jokewithpermutation

Input file: joke.in
Output file: joke.out
Joey had saved a permutation of integers from 1 to n in a text file. All the numbers were written as
decimal numbers without leading spaces.
Then Joe made a practical joke on her: he removed all the spaces in the file.
Help Joey to restore the original permutation after the Joe’s joke!
Input
The input file contains a single line with a single string — the Joey’s permutation without spaces.
The Joey’s permutation had at least 1 and at most 50 numbers.
Output
Write a line to the output file with the restored permutation. Don’t forget the spaces!
If there are several possible original permutations, write any one of them.
Sample input and output
joke.in

4111109876532

joke.out

4 1 11 10 9 8 7 6 5 3 2

题目链接:http://codeforces.com/gym/100553/attachments/download/2885/20142015-acmicpc-northeastern-european-regional-contest-neerc-14-en.pdf




题意:输入一排数字,所有的数字之间没有空格隔开,这些数字由1-n组,字符串的长度最少为1,最多为50。输出那一排数字,并用空格隔开。

这一题是一道经典的dfs搜索,搜索到的当前状态是字符串的第i个字符,如果这个数字没有被标记,那就搜索dfs(i+1,t+1);如果i+1<len的话,那么和后面那个字符一起组合成一个数字,如果这个数字不会超过n并且没有被标记的话,那么就搜索dfs(i+2,t+1);如果都不符合的话,那就return回溯。要注意dfs搜索不符合要求的话,要记得把数字的标记状态回溯恢复一下。当i==len时,说明其中一种情况已经搜索完毕也有可能搜索成了一种非正常状态,这时就要用一个for循环遍历判断是否说有的数字均被标记。




  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5. char s[55];
  6. int flag[55];
  7. int x[55];
  8. int len,n;
  9. int gg;
  10. void dfs(int i,int t);
  11. int main()
  12. {
  13. freopen("joke.in","r",stdin);
  14. freopen("joke.out","w",stdout);
  15. int i;
  16. memset(s,0,sizeof(s));
  17. memset(x,0,sizeof(x));
  18. memset(flag,0,sizeof(flag));
  19. scanf("%s",s);
  20. len=strlen(s);
  21. if(len<10) //优化,如果都是十以内
  22. {
  23. n=len;
  24. for(i=0; i<n-1; i++)
  25. printf("%c ",s[i]);
  26. printf("%c\n",s[i]);
  27. }
  28. else
  29. {
  30. n=(len-9)/2+9;
  31. gg=0;
  32. dfs(0,0);
  33. }
  34. return 0;
  35. }
  36. void dfs(int i,int t)
  37. {
  38. if(i==len)
  39. {
  40. int sign=1;
  41. for(int j=1; j<=n; j++)
  42. if(flag[j]==0)
  43. {
  44. sign=0;
  45. break;
  46. }
  47. if(sign)
  48. {
  49. for(int j=0; j<n-1; j++)
  50. printf("%d ",x[j]);
  51. printf("%d\n",x[n-1]);
  52. gg=1;
  53. }
  54. return;
  55. }
  56. if(gg==1) return;
  57. if(flag[s[i]-48]==0)
  58. {
  59. flag[s[i]-48]=1;
  60. x[t]=s[i]-48;
  61. dfs(i+1,t+1);
  62. if(gg) return;
  63. flag[s[i]-48]=0;
  64. }
  65. if((i+1)<len&&((s[i]-48)*10+(s[i+1]-48))<=n&&flag[(s[i]-48)*10+(s[i+1]-48)]==0)
  66. {
  67. flag[(s[i]-48)*10+(s[i+1]-48)]=1;
  68. x[t]=(s[i]-48)*10+(s[i+1]-48);
  69. dfs(i+2,t+1);
  70. if(gg) return;
  71. flag[(s[i]-48)*10+(s[i+1]-48)]=0;
  72. }
  73. return;
  74. }

  

codeforces 数字区分 搜索的更多相关文章

  1. 【BZOJ1853】幸运数字(搜索,容斥)

    [BZOJ1853]幸运数字(搜索,容斥) 题面 BZOJ 洛谷 题解 成功轰下洛谷rk1,甚至超越了一个打表选手 这题思路很明显吧,先搞出来所有范围内的合法数字,然后直接容斥, 容斥的话显然没有别的 ...

  2. BZOJ2393 & 1853 [Scoi2010]幸运数字 【搜索 + 容斥】

    题目 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,888都是" ...

  3. Codeforces 58E Expression (搜索)

    题意:给你一个可能不正确的算式a + b = c, 你可以在a,b,c中随意添加数字.输出一个添加数字最少的新等式x + y  = z; 题目链接 思路:来源于这片博客:https://www.cnb ...

  4. Codeforces 161E(搜索)

    要点 标签是dp但搜索一发就能过了. 因为是对称矩阵所以试填一下就是一个外层都填满了,因此搜索的深度其实不超过5. 显然要预处理有哪些素数.在这个过程中可以顺便再处理出一个\(vector:re[le ...

  5. Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)

    You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weak ...

  6. 【BZOJ1853】[Scoi2010]幸运数字 容斥原理+搜索

    Description 在中国,很多人都把6和8视为是幸运数字!lxhgww也这样认为,于是他定义自己的"幸运号码"是十进制表示中只包含数字6和8的那些号码,比如68,666,88 ...

  7. Cleaner Robot - CodeForces 589J(搜索)

    有一个M*N的矩阵,有一个会自动清洁的机器人,这个机器人会按照设定好的程序来打扫卫生,如果当前方向前面可以行走,那么直接走,如果不可以走那么会向右转动90度,然后回归上一步判断.求机器人最多能打扫的面 ...

  8. codeforces 14D(搜索+求树的直径模板)

    D. Two Paths time limit per test 2 seconds memory limit per test 64 megabytes input standard input o ...

  9. Lucene第二讲——索引与搜索

    一.Feild域 1.Field域的属性 是否分词:Tokenized 是:对该field存储的内容进行分词,分词的目的,就是为了索引. 否:不需要对field存储的内容进行分词,不分词,不代表不索引 ...

随机推荐

  1. 模块移除 命令rmmod 的实现

    // rmmod.c #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include < ...

  2. location search的中文加密

    最近项目中遇到一个这样问题,在页面跳转时,追加了location.search,有中文字符,但是在分享第二次时,这个链接无法获取中文字段,变成乱码. 仔细对比,发现在页面分享时,浏览器自动对中文进行了 ...

  3. 数组的es6新方法

    1.数组去重 var  changeReArr=(arr)=>{ return Array.from(new Set([1,2,2,3,5,4,5]))//利用set将[1,2,2,3,5,4, ...

  4. 材料订单不在IN_MO或者IN_SCFHEADER中

    select * from in_sfcheader where MO_ID IN('001600044481'); SELECT * FROM in_sfcheader_temp where MO_ ...

  5. Errors running builder 'Faceted Project Validation Builder' on project

    右键eclipse中的工程,选择properties,选择build,去掉出问题的validation校验项,重启eclipse即可.

  6. jquery mobile两个页面以及源码(登录与注册) 转

    ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 ...

  7. STL::map/multimap

    map: 默认根据 key 排序(从小到大),能够通过 backet operator(operator [ ]) 来获取元素,内部由二叉搜索树来实现(binary search trees). mu ...

  8. redismyadmin安装(支持redis4 集群模式)

    yum install php-pecl-redis https://github.com/daivem/RedisMyAdmin下载最新的安装包,解压yum install nginx php ph ...

  9. (转)学习ffmpeg官方示例transcoding.c遇到的问题和解决方法

    转自:https://blog.csdn.net/w_z_z_1991/article/details/53002416 Top 最近学习ffmpeg,官网提供的示例代码transcoding.c演示 ...

  10. A class of finite groups with abelian 2-Sylow subgroups By CHIH-HAN SAH

    Remark: 1.An element of a group which conjugate to its inverse is called a real element. If $G$ has ...