zoukankan      html  css  js  c++  java
  • Jokewithpermutation (DFS)

    Problem J. 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 joke.out


    4111109876532           4 1 11 10 9 8 7 6 5 3 2

    这是区域赛的题目!!!!

    DFS 暴力搜索 看了yan的代码  自己理解了一下 

    注意几点  1.vetor的使用 姿势

                 2.字符串长度判断最大n

                 3.一位或二位字符组成一个数

                 4.dfs姿势要熟练!!!!!

    #include<bits/stdc++.h>
    using namespace std;
    map<int,int>mp;
    vector<int>re;
    int s=0;
    int po;
    char a[105];
    int len;
    void dfs(int n)
    {
        if(n==len)
        {
            int flag=1;
            for(int i=1;i<=s;i++)
            {
                if(!mp[i])
                    flag=0;
            }
            //cout<<re.size()<<endl;
            if(flag)
            {
                for(unsigned int i=0;i<re.size()-1;i++)
                    printf("%d ",re[i]);
                printf("%d
    ",re[re.size()-1]);
                po=1;
            }
            return ;
        }
        if(po)
            return ;
        if(!mp[a[n]-'0'])
        {
            mp[a[n]-'0']=1;
            re.push_back(a[n]-'0');
            dfs(n+1);
            if(po)
                return ;
            re.pop_back();
            mp[a[n]-'0']=0;
        }
        if(!mp[(a[n]-'0')*10+a[n+1]-'0']&&((a[n]-'0')*10+a[n+1]-'0')<=s&&po==0&&n+1<len)
        {
            mp[(a[n]-'0')*10+a[n+1]-'0']=1;
            re.push_back((a[n]-'0')*10+a[n+1]-'0');
            dfs(n+2);
            if(po)
                return;
            mp[(a[n]-'0')*10+a[n+1]-'0']=0;
            re.pop_back();
        }
    }
    int main()
    {
         freopen("joke.in","r",stdin);
         freopen("joke.out","w",stdout);
        gets(a);
        len=strlen(a);
        re.clear();
        mp.clear();
        if(len<=9)
        {
            for(int i=0; i<len-1; i++)
                printf("%c ",a[i]);
            printf("%c
    ",a[len-1]);
            return 0;
        }
        po=0;
        s=(len-9)/2+9;
        dfs(0);
        return 0;
    }
  • 相关阅读:
    从零开始编写自己的C#框架(2)——开发前准备工作
    从零开始编写自己的C#框架(1)——前言
    SubSonic3.0.0.4.3源码包与调用Dll
    服务器安全检查指引——日常维护说明
    服务器安全部署文档
    linux使用rz、sz快速上传、下载文件
    PostgreSQL源码安装文档
    MySQL异步复制-加强版
    MySQL复制原理-加强版
    MySQL体系结构之物理文件
  • 原文地址:https://www.cnblogs.com/hsd-/p/4905301.html
Copyright © 2011-2022 走看看