Skip to content

Commit 4974ba4

Browse files
committed
srm 644 div 2 500
1 parent 2609b82 commit 4974ba4

File tree

2 files changed

+88
-0
lines changed

2 files changed

+88
-0
lines changed

LostCharacter.cpp

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 vector<int> vi;
14+
typedef vector<vi> vvi;
15+
typedef vector<string> vs;
16+
typedef vector<vs> vvs;
17+
18+
class LostCharacter{
19+
public:
20+
vs process(vs temp){
21+
for(string &s : temp){
22+
for(int i = 0; i < s.size(); i++) if(s[i] == '?') s[i] = 'z';
23+
}
24+
return temp;
25+
}
26+
vector <int> getmins(vector <string> str) {
27+
int n = str.size();
28+
vi res(n, 0);
29+
for(int j = 0; j < n; j++){
30+
string s = str[j];
31+
vs temp = str;
32+
for(int i = 0; i < s.size(); i++) if(s[i] == '?') s[i] = 'a';
33+
temp[j] = s;
34+
temp = process(temp);
35+
int k = 0;
36+
sort(temp.begin(), temp.end());
37+
for(k = 0; k < n && temp[k] != s; k++);
38+
res[j] = k;
39+
}
40+
return res;
41+
}
42+
43+
// BEGIN CUT HERE
44+
public:
45+
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(); }
46+
private:
47+
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(); }
48+
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; } }
49+
void test_case_0() { string Arr0[] = {"abc","bcd","cde","cdf","bbc"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0, 2, 3, 4, 1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(0, Arg1, getmins(Arg0)); }
50+
void test_case_1() { string Arr0[] = {"?ala","ara","baba"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0, 0, 1 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(1, Arg1, getmins(Arg0)); }
51+
void test_case_2() { string Arr0[] = {"a?","a","a","ab","aa"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {2, 0, 0, 3, 2 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(2, Arg1, getmins(Arg0)); }
52+
void test_case_3() { string Arr0[] = {"s?nu?ke","sm??eke","?sna?ke","so?th?e","shake??"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0, 1, 0, 2, 0 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(3, Arg1, getmins(Arg0)); }
53+
void test_case_4() { string Arr0[] = {"?","z?","zz?","zzz?","zzzz?","zzzzz?","zzzzzz?"}; vector <string> Arg0(Arr0, Arr0 + (sizeof(Arr0) / sizeof(Arr0[0]))); int Arr1[] = {0, 1, 2, 3, 4, 5, 6 }; vector <int> Arg1(Arr1, Arr1 + (sizeof(Arr1) / sizeof(Arr1[0]))); verify_case(4, Arg1, getmins(Arg0)); }
54+
55+
// END CUT HERE
56+
57+
};
58+
59+
// BEGIN CUT HERE
60+
int main(){
61+
62+
LostCharacter ___test;
63+
___test.run_test(-1);
64+
}
65+
// END CUT HERE

LostCharacter.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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>In this problem we are dealing with strings of lowercase English letters.
2+
When comparing our strings, we are using the standard lexicographic order.
3+
For example, "cat" &lt; "do" &lt; "dog" &lt; "done".
4+
(See Notes for a formal definition.)<br></br><br></br>
5+
6+
Suppose that L is a list of strings and that S is one of those strings.
7+
The <i>position</i> of S in L is the 0-based index of the first occurrence of S after L is sorted lexicographically.
8+
(Equivalently, the position of S in L can be defined as the number of strings in L that are strictly smaller than S.)<br></br><br></br>
9+
10+
For example, for the list L = {"abc", "bcd", "cde", "cdf", "bbc"}, the corresponding positions would be {0, 2, 3, 4, 1}.
11+
For the list L = {"a", "a", "b", "b", "c", "c"} the positions would be {0, 0, 2, 2, 4, 4}.<br></br><br></br>
12+
13+
Wolf Sothe has found an old list of strings.
14+
Some characters in the list were damaged beyond recognition.
15+
You are given the list as a vector &lt;string&gt; <b>str</b>.
16+
In <b>str</b>, the damaged characters are represented by the character '?' (question mark).<br></br><br></br>
17+
18+
Return a vector &lt;int&gt; with as many elements as <b>str</b>.
19+
For each valid i, the i-th element of the return value should be the smallest possible position of the i-th element of Sothe's list.</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>LostCharacter</td></tr><tr><td>Method:</td><td>getmins</td></tr><tr><td>Parameters:</td><td>vector &lt;string&gt;</td></tr><tr><td>Returns:</td><td>vector &lt;int&gt;</td></tr><tr><td>Method signature:</td><td>vector &lt;int&gt; getmins(vector &lt;string&gt; str)</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><tr><td>Stack limit (MB):</td><td>256</td></tr></table></td></tr><tr><td colspan="2"><h3>Notes</h3></td></tr><tr><td align="center" valign="top">-</td><td>Given two strings A and B, we say that A is smaller than B if either A is a proper prefix of B, or there is a non-negative integer i such that A[i]&lt;B[i] and for all j&lt;i we have A[j]=B[j].</td></tr><tr><td colspan="2"><h3>Constraints</h3></td></tr><tr><td align="center" valign="top">-</td><td><b>str</b> will contain between 1 and 50 elements, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each element in <b>str</b> will contain between 1 and 50 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Each character in <b>str</b> will be either a lowercase English letter ('a'-'z'), or '?'.</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;abc&quot;,&quot;bcd&quot;,&quot;cde&quot;,&quot;cdf&quot;,&quot;bbc&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0, 2, 3, 4, 1 }</pre></td></tr><tr><td><table><tr><td colspan="2">This is the first example from the problem statement.
20+
As there are no damaged letters, there is only one possible lexicographical order and you should return the corresponding positions.</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;?ala&quot;,&quot;ara&quot;,&quot;baba&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0, 0, 1 }</pre></td></tr><tr><td><table><tr><td colspan="2">In this test case we have one damaged character.
21+
If the damaged character was an 'a', the positions were {0,1,2}.
22+
Otherwise, the positions were {2,0,1}.
23+
Hence, the smallest possible position of "?ala" is 0, the smallest possible position of "ara" is 0, and the smallest possible position of "baba" is 1.</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;a?&quot;,&quot;a&quot;,&quot;a&quot;,&quot;ab&quot;,&quot;aa&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {2, 0, 0, 3, 2 }</pre></td></tr><tr><td><table><tr><td colspan="2">Sothe's list may contain duplicates.</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;s?nu?ke&quot;,&quot;sm??eke&quot;,&quot;?sna?ke&quot;,&quot;so?th?e&quot;,&quot;shake??&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0, 1, 0, 2, 0 }</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;?&quot;,&quot;z?&quot;,&quot;zz?&quot;,&quot;zzz?&quot;,&quot;zzzz?&quot;,&quot;zzzzz?&quot;,&quot;zzzzzz?&quot;}</pre></td></tr></table></td></tr><tr><td><pre>Returns: {0, 1, 2, 3, 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)