zoukankan      html  css  js  c++  java
  • 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
    Problem description

    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
    4111109876532
    Sample Output
    4 1 11 10 9 8 7 6 5 3 2
    Problem Source
    NEERC 2014

    Submit   Discuss   Judge Status  Problems  Ranklist 

    题目大意:将一串完整的字符串分成1~n个数。将空格补进去,并将其输出。

    解题思路:1、注意这个n是可以求出来的

         2、一个数字一个数字比较,只要满足这个数字小于n,还有保证这个数字没有访问过就ok啦

       

    详见代码。

     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 
     5 using namespace std;
     6 
     7 int n,flag;
     8 char ch[110];
     9 int ans[110];
    10 bool vis[110];
    11 
    12 bool dfs(int i,int k)
    13 {
    14     if (flag==1)
    15         return true;
    16     if (k==n+1)
    17     {
    18         for (int i=1;i<k-1;i++)
    19         {
    20             printf ("%d ",ans[i]);
    21         }
    22         printf ("%d
    ",ans[k-1]);
    23         flag=1;
    24         return true;
    25     }
    26     if (ch[i]-'0'<=n&&!vis[ch[i]-'0']&&ch[i]-'0'>0)
    27     {
    28         ans[k]=ch[i]-'0';
    29         vis[ch[i]-'0']=1;
    30         if(dfs(i+1,k+1)) return true;
    31         vis[ch[i]-'0']=0;
    32     }
    33     if ((ch[i]-'0')*10+ch[i+1]-'0'<=n&&(ch[i]-'0')*10+ch[i+1]-'0'>9&&!vis[(ch[i]-'0')*10+ch[i+1]-'0'])
    34     {
    35         ans[k]=(ch[i]-'0')*10+ch[i+1]-'0';
    36         vis[(ch[i]-'0')*10+ch[i+1]-'0']=1;
    37         if(dfs(i+2,k+1)) return true;
    38         vis[(ch[i]-'0')*10+ch[i+1]-'0']=0;
    39     }
    40     return false;
    41 }
    42 
    43 int main()
    44 {
    45     while (scanf("%s",ch)!=EOF)
    46     {
    47         flag=0;
    48         memset(ans,0,sizeof(ans));
    49         memset(vis,0,sizeof(vis));
    50         int len=strlen(ch);
    51         if (len>9)
    52             n=(len-9)/2+9;
    53         else
    54             n=len;
    55         dfs(0,1);//dfs();
    56     }
    57     return 0;
    58 }
  • 相关阅读:
    java基本数据类型
    解决Eclipse导入项目工程出现The project was not built since错误
    解决java错误“编码 GBK 的不可映射字符”
    helloworld每一个程序员的开始!
    java环境变量path也配置了,但还是显示javac不是内部或外部命令的解决办法
    WAS8.5.5根据CVE-2019-4279漏洞升级
    ansible 配置数据源源头为163
    升级openssh
    ansible-1 参数常用模块
    xlsxwriter模块详解
  • 原文地址:https://www.cnblogs.com/qq-star/p/4695254.html
Copyright © 2011-2022 走看看