2020牛客国庆集训派对day4 Jokewithpermutation

What Goes Up Must Come Down

https://ac.nowcoder.com/acm/contest/7831/F

Jokewithpermutation

题目描述

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! 输入描述: 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. 输出描述: 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.

示例1
输入
复制

4111109876532

输出
复制

4 1 11 10 9 8 7 6 5 3 2

备注:
Author: Mikhail Dvorkin

题意:

给你一串数,这串数字为n的全排列,问怎么将数分段格,使得成为n的全排列
看样例4111109876532,可以为4 1 11 10 9 8 7 6 5 3 2

题解:

队友做得,dfs暴力即可,就是假设当为一位数或者两位数时,看看情况
详细看代码吧

代码:

#include<bits/stdc++.h>
#define maxn 6000
using namespace std;
char a[maxn];
vector<int>ans;
bool vis[maxn];
unordered_map<int,int>p;
bool f=0;
int s;
void dfs(int n){
    if(f)
    return ;
    int size=strlen(a+1);
    if(n>size){
        printf("%d",ans[0]);
        for(int i=1;i<ans.size();i++){
            printf(" %d",ans[i]);
        }
        f=1;
        return ;
    }
    if(p[a[n]-'0']==0){
        p[a[n]-'0']=1;
        ans.push_back(a[n]-'0');
        dfs(n+1);
        ans.pop_back();
        p[a[n]-'0']=0;
    }
    if(f)
    return ;
    if(n<=size-1&&p[(a[n]-'0')*10+(a[n+1]-'0')]==0&&(a[n]-'0')*10+(a[n+1]-'0')<=s){
        p[(a[n]-'0')*10+(a[n+1]-'0')]=1;
        ans.push_back((a[n]-'0')*10+(a[n+1]-'0'));
        dfs(n+2);
        ans.pop_back();
        p[(a[n]-'0')*10+(a[n+1]-'0')]=0;
    }
    if(f)
    return ;
}
int main(){
    scanf("%s",a+1);
    s=strlen(a+1)>9?(strlen(a+1)-9)/2+9:strlen(a+1);
    dfs(1);
    return 0;
}
全部评论

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
07-02 22:46
门头沟学院 Java
码农索隆:hr:“管你投没投,先挂了再说”
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务