关于队里面最菜的在博客打卡第十七天这件事

题目链接:Attachments - 2014-2015 ACM-ICPC Northeastern European Regional Contest (NEERC 14) - Codeforces

这是一道神奇的题,题意就是给你一个数的某种排列,然后让他们没有间隔的输出出来,你需要找的就是满足条件的任意原排列(当时想了半天的贪心,结果还是用dfs了,谁知道居然过了哈哈(后来才发现就是dfs

关于队里面最菜的在博客打卡第十七天这件事_第1张图片

 ac代码

#include
#include
using namespace std;
const int N = 110;
int a[N],ans[N];
bool st[N];
bool book[N];
string s;
int m ;
bool find(int u , int step)
{
    if(step == m)return true;
    int num = 0 ;
    for(int i = 0 ; i < s.size() ; i ++)
    {
        if(u <= 9 && !st[i])
        {
            num = a[i];
            if(num != u)continue;
            st[i] = true;
            ans[i] = num;
            bool f = find(u-1,step+1);
            if(f)return true;
            st[i] = false;
        }
        else if(!st[i] && i < s.size() - 1 && !st[i + 1])
        {
            num = a[i] * 10 + a[i+1];
            if(num != u)continue;
            st[i] = st[i + 1] = true;
            ans[i] = ans[i+1] = num;
            bool f = find(u - 1, step + 1);
            if(f)return true;
            st[i] = st[i + 1] = false;
        }
    }
    return false;
}
int main()
{   
  freopen("joke.in","r",stdin);
  freopen("joke.out","w",stdout);
  cin >> s;
  for(int i = 0 ; i < s.size() ; i ++)
  {
        a[i] = s[i] - '0';
  }
  if(s.size() <= 9)m = s.size();
  else m = (s.size() - 9)/2 + 9;
  find(m,0);
  for(int i = 0 ; i < s.size() ; i ++)
  {
    if(!book[ans[i]])cout << ans[i] <<' ';
     book[ans[i]] = true;
  }
  return 0 ;

}

你可能感兴趣的:(关于队里面最菜的在博客打卡第十七天这件事)