Skip to content

Commit 82c2268

Browse files
committed
Barclays 200
1 parent 21e3f88 commit 82c2268

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

Diff for: TranspositionKey.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
#include <cctype>
12+
using namespace std;
13+
typedef pair<int,int> pi;
14+
typedef set<int> si;
15+
typedef vector<int> vi;
16+
typedef vector<vi> vvi;
17+
typedef vector<string> vs;
18+
19+
class TranspositionKey{
20+
public:
21+
vector <int> makeKey(string text) {
22+
string s;
23+
for(char c : text)
24+
if(isalpha(c)) s.push_back(tolower(c));
25+
int start = 0, n = s.size();
26+
vi res(n);
27+
cout<<s<<endl;
28+
for(int i = 'a'; i <= 'z'; i++){
29+
char c = char(i);
30+
for(int j = 0; j < n; j++)
31+
if(s[j] == c) res[j] = ++start;
32+
}
33+
return res;
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(); }
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 vector <int> &Expected, const vector <int> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } }
42+
void test_case_0() { string Arg0 = "aaa"; int Arr1[] = {1, 2, 3 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, makeKey(Arg0)); }
43+
void test_case_1() { string Arg0 = "ywedkcjs"; int Arr1[] = {8, 7, 3, 2, 5, 1, 4, 6 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, makeKey(Arg0)); }
44+
void test_case_2() { string Arg0 = "Quoth the raven, Nevermore."; int Arr1[] = {14, 20, 12, 18, 7, 19, 8, 2, 15, 1, 21, 3, 10, 11, 4, 22, 5, 16, 9, 13, 17, 6 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, makeKey(Arg0)); }
45+
void test_case_3() { string Arg0 = "Fuzzy wuzzy was a bear."; int Arr1[] = {6, 9, 15, 16, 13, 11, 10, 17, 18, 14, 12, 1, 8, 2, 4, 5, 3, 7 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, makeKey(Arg0)); }
46+
void test_case_4() { string Arg0 = "UCC Event"; int Arr1[] = {7, 1, 2, 3, 8, 4, 5, 6 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(4, Arg1, makeKey(Arg0)); }
47+
48+
// END CUT HERE
49+
50+
};
51+
52+
// BEGIN CUT HERE
53+
int main(){
54+
55+
TranspositionKey ___test;
56+
___test.run_test(-1);
57+
}
58+
// END CUT HERE

Diff for: TranspositionKey.html

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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>Some coding systems involve using a set of known text to create a transposition key that determines how a message will be scrambled. Unfortunately, humans are error prone and frequently make mistakes, so it will be your job to write a function to do it properly.</p>
2+
3+
<p>To create a transposition key from text, take the first letter in the alphabet
4+
that appears in the text. Number each occurrence of this letter in the text
5+
from left to right, starting with 1, 2, 3, etc. Ignore case. Then take the
6+
next letter in the alphabet that appears in the text and again number each
7+
occurrence of this letter from left to right, continuing the numbering from
8+
where you previously left off. Continue until done. The following example
9+
illustrates:</p>
10+
11+
<pre>
12+
Quoth the raven, Nevermore.
13+
14+
The first letter in the alphabet which appears in this text is a. Thus:
15+
16+
1
17+
Q U O T H T H E R A V E N N E V E R M O R E
18+
19+
The next letter that appears is e. Numbering the e's, we get:
20+
21+
2 1 3 4 5 6
22+
Q U O T H T H E R A V E N N E V E R M O R E
23+
24+
Next comes h:
25+
26+
7 8 2 1 3 4 5 6
27+
Q U O T H T H E R A V E N N E V E R M O R E
28+
29+
Continuing through the last letter, v, gives the finished key:
30+
31+
14 20 12 18 7 19 8 2 15 1 21 3 10 11 4 22 5 16 9 13 17 6
32+
Q U O T H T H E R A V E N N E V E R M O R E
33+
</pre>
34+
35+
<p>Your function should return the numbers across the top.</p>
36+
37+
</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>TranspositionKey</td></tr><tr><td>Method:</td><td>makeKey</td></tr><tr><td>Parameters:</td><td>string</td></tr><tr><td>Returns:</td><td>vector &lt;int&gt;</td></tr><tr><td>Method signature:</td><td>vector &lt;int&gt; makeKey(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>256</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 have a length from 0 to 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>- <b>text</b> will contain only letters, spaces, commas, and periods.</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;aaa&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: {1, 2, 3 }</pre></td></tr><tr><td><table><tr><td colspan="2">Note we go from left to right when seeing duplicates of the same letter.</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;ywedkcjs&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: {8, 7, 3, 2, 5, 1, 4, 6 }</pre></td></tr><tr><td><table><tr><td colspan="2">No duplicates here.</td></tr></table></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;Quoth the raven, Nevermore.&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: {14, 20, 12, 18, 7, 19, 8, 2, 15, 1, 21, 3, 10, 11, 4, 22, 5, 16, 9, 13, 17, 6 }</pre></td></tr><tr><td><table><tr><td colspan="2">This is the example from the problem statement.</td></tr></table></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;Fuzzy wuzzy was a bear.&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: {6, 9, 15, 16, 13, 11, 10, 17, 18, 14, 12, 1, 8, 2, 4, 5, 3, 7 }</pre></td></tr><tr><td><table><tr><td colspan="2"></td></tr></table></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;UCC Event&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: {7, 1, 2, 3, 8, 4, 5, 6 }</pre></td></tr><tr><td><table><tr><td colspan="2"></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>

0 commit comments

Comments
 (0)