• 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;
    }
  • 相关阅读:
    java学习(一)
    linux下java环境配置
    移动平台开发团队展示
    VS的安装项目:解决安装后注册表中程序信息InstallLocation为空的问题
    .NET将数据导出到Excel表(如何指定默认文件名)
    Linux Shell编程入门
    http与WebSocket
    git push时报错filename too long的解决
    js中传统事件绑定模拟现代事件处理
    对三次握手的理解
  • 原文地址:https://www.cnblogs.com/hsd-/p/4905301.html
Copyright © 2020-2023  润新知