Joke with permutation

Joke with permutation
Time Limit: 3000ms, Special Time Limit:7500ms, Memory Limit:65536KB
Total submit users: 85, Accepted users: 57
Problem 13341 : Special judge
Problem description

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
4111109876532
Sample Output
4 1 11 10 9 8 7 6 5 3 2
题目描述:给你一段数字串,是1~n数字的乱序,要你将其分开输出

解题思路:用字符串数组s存储数字串,暴力搜索,但是这里要注意的是需要求得n通过简单推导n = len>9? (len-2)+9 : 9;

把n作为参数传入dfs(n),每次在s串中查找n的字符表示用一个标记数组vis标记为n;n=0作为dfs的跳出条件用mark标记。

这里的mark变量相当重要

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
using namespace std;
const int maxn = 100001;
int len;
int mark;
int vis[maxn];
char s[maxn];
void dfs(int n){
	if(n==0)
	{
		mark = 1;
	    return ;
	}
	if(n > 9)
	{
		char t1 = n/10+'0';
		char t2 = n%10+'0';
		for(int i = 0;i < len;++ i)
		{
			if(s[i]==t1&&s[i+1]==t2&&!vis[i]&&!vis[i+1])
			{
				vis[i] = vis[i+1]=n;
				dfs(n-1);
				if(mark) return ;	
				vis[i] = vis[i+1]=0;
			}
		}
	}
	else 
	{
		char t = n+'0';
		for(int i = 0;i < len;++ i)
		{
			if(s[i]==t&&!vis[i])
			{
				vis[i] = n;		
				dfs(n-1);
				if(mark) return ;	
				vis[i] = 0;
			}
		}
	}
}
int main()
{
	cin >> s;
	len = strlen(s);
	mark = 0;
	int num = len > 9 ? ((len-9)/2+9) : len;
	// cout << num << endl;
	memset(vis,0,sizeof(vis));
	dfs(num);
	cout << s[0];
	for(int i = 1;i < len;++ i)
	{
		if(vis[i]==vis[i-1])
			cout << s[i];
		else
			cout << " " << s[i];
	}
	cout << endl;
}
</textarea>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值