Codeforces, codeforce

Source: Internet
Author: User

Codeforces, codeforce

Jokewithpermutation

Input parameter le: joke. in
Output role le: joke. out
Joey had saved a permutation of integers from 1 to n in a text folder le. All the numbers were written
Decimal numbers without leading spaces.
Then Joe made a practical joke on her: he removed all the spaces in the specified le.
Help Joey to restore the original permutation after the Joe's joke!
Input
The input parameter le 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 parameter le 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

 

Link: http://codeforces.com/gym/100553/attachments/download/2885/20142015-acmicpc-northeastern-european-regional-contest-neerc-14-en.pdf

 

Enter a row of numbers. All numbers are separated by no space. These numbers are in 1-N groups. the string length is at least 1 and at most 50. Output the numbers in the row and separate them with spaces.

 

This is a classic dfs search. The current status is the I character of the string. If this number is not marked, search for dfs (I + 1, t + 1); if I + 1 <len, then combine it with the character following it into a number. If the number does not exceed n and is not marked, search for dfs (I + 2, t + 1). If none of them match, return backtracing. Note that if the dfs search does not meet the requirements, remember to restore the mark status of the number. When I = len, it indicates that one of the cases has been searched and may become abnormal, in this case, we need to use a for loop traversal to determine whether all numbers are marked.

 

# 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) // optimized, {n = len; for (I = 0; I <n-1; I ++) printf ("% c", s [I]); printf ("% c \ n", s [I]);} else {n = (len-9)/2 + 9; gg = 0; dfs );} 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 \ n", 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 ;}

  

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.