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;
    }
  • 相关阅读:
    [Ansible]copy 模块
    [Ansible]script模块
    [Ansible]command shell模块
    [Ansible]Systemd 模块
    [Ansible]YUM 模块
    [Ansible]yum_repository模块 添加 删除yum源
    [Ceph]osd 无法启动 start request repeated too quickly for ceph-osd@1.service
    [Ceph]pool 删除 Error EPERM: pool deletion is disabled; you must first set the mon_allow_pool_delete config option to true before you can destroy a pool
    题解 烷基计数 加强版 加强版
    Polya 定理 学习笔记
  • 原文地址:https://www.cnblogs.com/hsd-/p/4905301.html
Copyright © 2011-2022 走看看