zoukankan      html  css  js  c++  java
  • codeforces 数字区分 搜索

    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循环遍历判断是否说有的数字均被标记。




    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    char s[55];
    int flag[55];
    int x[55];
    int len,n;
    int gg;
    void dfs(int i,int t);
    int main()
    {
        freopen("joke.in","r",stdin);
        freopen("joke.out","w",stdout);
        int i;
        memset(s,0,sizeof(s));
        memset(x,0,sizeof(x));
        memset(flag,0,sizeof(flag));
        scanf("%s",s);
        len=strlen(s);
        if(len<10) //优化,如果都是十以内
        {
            n=len;
            for(i=0; i<n-1; i++)
                printf("%c ",s[i]);
            printf("%c
    ",s[i]);
        }
        else
        {
            n=(len-9)/2+9;
            gg=0;
            dfs(0,0);
        }
        return 0;
    }
    void dfs(int i,int t)
    {
        if(i==len)
        {
            int sign=1;
            for(int j=1; j<=n; j++)
                if(flag[j]==0)
                {
                    sign=0;
                    break;
                }
            if(sign)
            {
                for(int j=0; j<n-1; j++)
                    printf("%d ",x[j]);
                printf("%d
    ",x[n-1]);
                gg=1;
            }
            return;
        }
        if(gg==1) return;
        if(flag[s[i]-48]==0)
        {
            flag[s[i]-48]=1;
            x[t]=s[i]-48;
            dfs(i+1,t+1);
            if(gg) return;
            flag[s[i]-48]=0;
        }
        if((i+1)<len&&((s[i]-48)*10+(s[i+1]-48))<=n&&flag[(s[i]-48)*10+(s[i+1]-48)]==0)
        {
            flag[(s[i]-48)*10+(s[i+1]-48)]=1;
            x[t]=(s[i]-48)*10+(s[i+1]-48);
            dfs(i+2,t+1);
            if(gg) return;
            flag[(s[i]-48)*10+(s[i+1]-48)]=0;
        }
        return;
    }
    

      

    I am a slow walker,but I never walk backwards.
  • 相关阅读:
    Oracle11g: simple sql script examples
    MySQL5.7: datetime
    SQL Server: Datetime,Datetime2
    np.dot()、np.multiply()、np.matmul()方法以及*和@运算符的用法总结
    机器学习基础---机器学习系统设计
    机器学习基础---机器学习诊断法
    机器学习基础---神经网络(调试优化)(随机初始化、梯度检测)
    机器学习作业---神经网络实现多类分类
    机器学习基础---神经网络(属于逻辑回归)(构建训练集以及参数学习)
    机器学习基础---神经网络(属于逻辑回归)(构建假设函数)
  • 原文地址:https://www.cnblogs.com/GeekZRF/p/4918347.html
Copyright © 2011-2022 走看看