Skip to content

Commit d0d7438

Browse files
committed
Barclays Challenge 300
1 parent 7a11e96 commit d0d7438

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

Scramble.cpp

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <vector>
4+
#include <algorithm>
5+
#include <set>
6+
#include <map>
7+
#include <queue>
8+
#include <cstring>
9+
#include <climits>
10+
#include <cstdio>
11+
using namespace std;
12+
typedef pair<int,int> pi;
13+
typedef set<int> si;
14+
typedef vector<int> vi;
15+
typedef vector<vi> vvi;
16+
typedef vector<string> vs;
17+
18+
class Scramble{
19+
public:
20+
string scrambleWord(string text) {
21+
int n = text.size();
22+
string startS = string(1, text[0]), endS = string(1, text[n-1]);
23+
string temp = text.substr(1, n-2);
24+
sort(temp.begin(), temp.end());
25+
int start = 1;
26+
while(!temp.empty()) {
27+
string t = string(1, temp[0]);
28+
if(start) startS += t;
29+
else endS = t + endS;
30+
temp = temp.substr(1);
31+
start = (start + 1)%2;
32+
}
33+
return startS + endS;
34+
}
35+
36+
// BEGIN CUT HERE
37+
public:
38+
void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); if ((Case == -1) || (Case == 6)) test_case_6(); }
39+
private:
40+
template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); }
41+
void verify_case(int Case, const string &Expected, const string &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } }
42+
void test_case_0() { string Arg0 = "alphabet"; string Arg1 = "aaelphbt"; verify_case(0, Arg1, scrambleWord(Arg0)); }
43+
void test_case_1() { string Arg0 = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx"; string Arg1 = "aabcdefghijklmnopqrstuvwyzxwvutsrqponmlkjihgfedcbx"; verify_case(1, Arg1, scrambleWord(Arg0)); }
44+
void test_case_2() { string Arg0 = "aa"; string Arg1 = "aa"; verify_case(2, Arg1, scrambleWord(Arg0)); }
45+
void test_case_3() { string Arg0 = "abdfheca"; string Arg1 = "abdfheca"; verify_case(3, Arg1, scrambleWord(Arg0)); }
46+
void test_case_4() { string Arg0 = "aaaaaaaaa"; string Arg1 = "aaaaaaaaa"; verify_case(4, Arg1, scrambleWord(Arg0)); }
47+
void test_case_5() { string Arg0 = "uodoutivesbegckw"; string Arg1 = "ubdeiosuvtokgecw"; verify_case(5, Arg1, scrambleWord(Arg0)); }
48+
void test_case_6() { string Arg0 = "zaabz"; string Arg1 = "zabaz"; verify_case(6, Arg1, scrambleWord(Arg0)); }
49+
50+
// END CUT HERE
51+
52+
};
53+
54+
// BEGIN CUT HERE
55+
int main(){
56+
57+
Scramble ___test;
58+
___test.run_test(-1);
59+
}
60+
// END CUT HERE

Scramble.html

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<html><body bgcolor="#000000" text="#ffffff"><table><tr><td colspan="2"><h3>Problem Statement</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><p>
2+
<i>Did you konw taht slcrabmed wdros can be raed wtih vrey ltlite erfoft?</i>
3+
Oh, sorry.
4+
Did you know that scrambled words can be read with very little effort?
5+
We can interpret most heavily mangled words because our brains &quot;see&quot; the similarity with the original words and catch the meaning of the text. It is claimed that as long as the first and last letters in every word remain unchanged, we can shuffle the rest of the letters within each word to obtain a reasonably readable text. We want to try the following strategy of scrambling letters in words:
6+
<ol>
7+
<li>Leave the first and last letters in their original positions, and remove the rest of the letters. For example, &quot;alphabet&quot; becomes &quot;a______t&quot; (underscores represent empty positions).</li>
8+
9+
<li>Sort the removed letters alphabetically.
10+
In this example, the removed letters are &quot;lphabe&quot;, and they are sorted to become &quot;abehlp&quot;.</li>
11+
12+
<li>If any removed letters still exist, take the first one and move it to the leftmost empty position in the string.
13+
In this case, the string becomes &quot;aa_____t&quot;, and the remaining removed letters are now &quot;behlp&quot;.</li>
14+
15+
<li>If any removed letters still exist, take the first one and move it to the rightmost empty position in the string.
16+
So, the string becomes &quot;aa____bt&quot;, and the remaining removed letters are now &quot;ehlp&quot;.</li>
17+
18+
<li>Repeat steps 3 and 4 until no removed letters remain.
19+
In this example, the string becomes: &quot;aae___bt&quot; -&gt; &quot;aae__hbt&quot; -&gt; &quot;aael_hbt&quot; -&gt; &quot;aaelphbt&quot;.</li>
20+
</ol>
21+
</p>
22+
<p>You are given a string <b>text</b> containing a lowercase word.
23+
Return the scrambled word following the described procedure.
24+
</p></td></tr><tr><td colspan="2"><h3>Definition</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Class:</td><td>Scramble</td></tr><tr><td>Method:</td><td>scrambleWord</td></tr><tr><td>Parameters:</td><td>string</td></tr><tr><td>Returns:</td><td>string</td></tr><tr><td>Method signature:</td><td>string scrambleWord(string text)</td></tr><tr><td colspan="2">(be sure your method is public)</td></tr></table></td></tr><tr><td colspan="2"><h3>Limits</h3></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td>Time limit (s):</td><td>2.000</td></tr><tr><td>Memory limit (MB):</td><td>64</td></tr></table></td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>text</b> will contain between 2 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td><b>text</b> will contain only lowercase letters ('a'-'z').</td></tr><tr><td colspan="2"><h3>Examples</h3></td></tr><tr><td align="center" nowrap="true">0)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;alphabet&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;aaelphbt&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">Example from the problem statement.</td></tr></table></td></tr></table></td></tr><tr><td align="center" nowrap="true">1)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwx&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;aabcdefghijklmnopqrstuvwyzxwvutsrqponmlkjihgfedcbx&quot;</pre></td></tr><tr><td></td></tr></table></td></tr><tr><td align="center" nowrap="true">2)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;aa&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;aa&quot;</pre></td></tr><tr><td></td></tr></table></td></tr><tr><td align="center" nowrap="true">3)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;abdfheca&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;abdfheca&quot;</pre></td></tr><tr><td></td></tr></table></td></tr><tr><td align="center" nowrap="true">4)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;aaaaaaaaa&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;aaaaaaaaa&quot;</pre></td></tr><tr><td></td></tr></table></td></tr><tr><td align="center" nowrap="true">5)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;uodoutivesbegckw&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;ubdeiosuvtokgecw&quot;</pre></td></tr><tr><td></td></tr></table></td></tr><tr><td align="center" nowrap="true">6)</td><td></td></tr><tr><td>&#160;&#160;&#160;&#160;</td><td><table><tr><td><table><tr><td><pre>&quot;zaabz&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;zabaz&quot;</pre></td></tr><tr><td></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>

0 commit comments

Comments
 (0)