forked from daiwb/Algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPermutationSignature.html
More file actions
13 lines (11 loc) · 3.94 KB
/
Copy pathPermutationSignature.html
File metadata and controls
13 lines (11 loc) · 3.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
<html><body bgcolor="#cccccc" text="#000000"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>    </td><td><p>
The <i>signature</i> of a permutation is a string that is computed as follows: for each pair of consecutive elements of the permutation, write down the letter '<tt>I</tt>' (increasing) if the second element is greater than the first one, otherwise write down the letter '<tt>D</tt>' (decreasing).
</p>
<p>
For example, the signature of the permutation {3,1,2,7,4,6,5} is "<tt>DIIDID</tt>".
</p>
<p>
Your task is to reverse this computation: You are given a string <b>signature</b> containing the signature of a permutation.
Find and return the lexicographically smallest permutation with the given signature.
If no such permutation exists, return an empty vector <int> instead.
</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>    </td><td><table><tr><td>Class:</td><td>PermutationSignature</td></tr><tr><td>Method:</td><td>reconstruct</td></tr><tr><td>Parameters:</td><td>string</td></tr><tr><td>Returns:</td><td>vector <int></td></tr><tr><td>Method signature:</td><td>vector <int> reconstruct(string signature)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td>    </td></tr><tr><td></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>For any positive integer N, a permutation of N elements is a sequence of length N that contains each of the integers 1 through N exactly once.</td></tr><tr><td align="center" valign="top">-</td><td>To compare two permutations A and B, find the smallest index i such that A[i] and B[i] differ. If A[i] < B[i], we say that A is lexicographically smaller than B, and vice versa.</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>signature</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each character in <b>signature</b> will be either '<tt>I</tt>' or '<tt>D</tt>'.</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"IIIII"</pre></td></tr></table></td></tr><tr><td><pre>Returns: {1, 2, 3, 4, 5, 6 }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"DI"</pre></td></tr></table></td></tr><tr><td><pre>Returns: {2, 1, 3 }</pre></td></tr><tr><td><table><tr><td colspan="2">There are two permutations with this signature: {3,1,2} and {2,1,3}. You must return the lexicographically smaller one.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"IIIID"</pre></td></tr></table></td></tr><tr><td><pre>Returns: {1, 2, 3, 4, 6, 5 }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>    </td><td><table><tr><td><table><tr><td><pre>"DIIDID"</pre></td></tr></table></td></tr><tr><td><pre>Returns: {2, 1, 3, 5, 4, 7, 6 }</pre></td></tr><tr><td><table><tr><td colspan="2">This is the signature from the problem statement. Note that the correct answer is not the permutation from the problem statement.</td></tr></table></td></tr></table></td></tr></table><p>This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved. </p></body></html>