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. |
Input |
The input file contains a single line with a single string — the Joey’s permutation without spaces. |
Output |
Write a line to the output file with the restored permutation. Don’t forget the spaces! |
Sample Input |
4111109876532 |
Sample Output |
4 1 11 10 9 8 7 6 5 3 2 |
解题思路:用字符串数组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>