Skip to content

Commit e2b2c6e

Browse files
committed
srm 165 div 2 1150
1 parent 3d492e8 commit e2b2c6e

File tree

2 files changed

+96
-0
lines changed

2 files changed

+96
-0
lines changed

ShortPalindromes.cpp

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
string dp[50][50];
18+
19+
class ShortPalindromes{
20+
public:
21+
string shortest(string s) {
22+
int n = s.size(), strokes = 0;
23+
if(n == 1) return s;
24+
for(int i = 0; i < n; i++){
25+
dp[i][i] = s.substr(i, 1);
26+
}
27+
for(int l = 2; l <= n; l++){
28+
for(int i = 0; i+l <= n; i++){
29+
int j = i+l-1;
30+
string res = string(50, 'z');
31+
// cout<<s.substr(i, l)<<" ";
32+
if(s[i] == s[j]){
33+
string t = dp[i+1][j-1];
34+
t.insert(t.begin(), s[i]);
35+
t.insert(t.end(), s[j]);
36+
if(t.size() < res.size() || (t.size() == res.size() && t < res)) res = t;
37+
// cout<<t<<" ";
38+
}
39+
string t = dp[i+1][j];
40+
t.insert(t.begin(), s[i]);
41+
t.insert(t.end(), s[i]);
42+
// cout<<t<<" ";
43+
if(t.size() < res.size() || (t.size() == res.size() && t < res)) res = t;
44+
t = dp[i][j-1];
45+
t.insert(t.begin(), s[j]);
46+
t.insert(t.end(), s[j]);
47+
// cout<<t<<" ";
48+
if(t.size() < res.size() || (t.size() == res.size() && t < res)) res = t;
49+
dp[i][j] = res;
50+
// cout<<dp[i][j]<<endl;
51+
}
52+
}
53+
return dp[0][n-1];
54+
}
55+
56+
// BEGIN CUT HERE
57+
public:
58+
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(); }
59+
private:
60+
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(); }
61+
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; } }
62+
void test_case_0() { string Arg0 = "RACE"; string Arg1 = "ECARACE"; verify_case(0, Arg1, shortest(Arg0)); }
63+
void test_case_1() { string Arg0 = "TOPCODER"; string Arg1 = "REDTOCPCOTDER"; verify_case(1, Arg1, shortest(Arg0)); }
64+
void test_case_2() { string Arg0 = "Q"; string Arg1 = "Q"; verify_case(2, Arg1, shortest(Arg0)); }
65+
void test_case_3() { string Arg0 = "MADAMIMADAM"; string Arg1 = "MADAMIMADAM"; verify_case(3, Arg1, shortest(Arg0)); }
66+
void test_case_4() { string Arg0 = "ALRCAGOEUAOEURGCOEUOOIGFA"; string Arg1 = "AFLRCAGIOEOUAEOCEGRURGECOEAUOEOIGACRLFA"; verify_case(4, Arg1, shortest(Arg0)); }
67+
68+
// END CUT HERE
69+
70+
};
71+
72+
// BEGIN CUT HERE
73+
int main(){
74+
75+
ShortPalindromes ___test;
76+
___test.run_test(-1);
77+
}
78+
// END CUT HERE

ShortPalindromes.html

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
A palindrome is a string that is spelled the same forward and backwards. Given a string
3+
<b>base</b> that may or may not be a palindrome, we can always force <b>base</b> to be a palindrome by adding
4+
letters to it. For example, given the word &quot;RACE&quot;, we could add the letters &quot;CAR&quot; to its back to get &quot;RACECAR&quot; (quotes for clarity only). However, we are not restricted to adding letters at the back. For example, we
5+
could also add the letters &quot;ECA&quot; to the front to get &quot;ECARACE&quot;. In fact, we can add letters anywhere in the word, so we
6+
could also get &quot;ERCACRE&quot; by adding an 'E' at the beginning, a 'C' after the 'R', and another 'R' before the final 'E'.
7+
Your task is to make <b>base</b> into a palindrome by adding as few letters
8+
as possible and return the resulting string. When there is more than one palindrome of minimal length that can be made, return the lexicographically earliest (that is, the one that occurs first in alphabetical order).
9+
</p>
10+
11+
</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>ShortPalindromes</td></tr><tr><td>Method:</td><td>shortest</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 shortest(string base)</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>base</b> contains between 1 and 25 characters, inclusive.</td></tr><tr><td align="center" valign="top">-</td><td>Every character in <b>base</b> is an uppercase letter ('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;RACE&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;ECARACE&quot;</pre></td></tr><tr><td><table><tr><td colspan="2">To make &quot;RACE&quot; into a palindrome, we must add at least three letters. However, there are
12+
eight ways to do this by adding exactly three letters:
13+
<pre>
14+
&quot;ECARACE&quot; &quot;ECRARCE&quot; &quot;ERACARE&quot; &quot;ERCACRE&quot;
15+
&quot;RACECAR&quot; &quot;RAECEAR&quot; &quot;REACAER&quot; &quot;RECACER&quot;
16+
</pre>
17+
Of these alternatives, &quot;ECARACE&quot; is the lexicographically earliest.
18+
</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;TOPCODER&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;REDTOCPCOTDER&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;Q&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;Q&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;MADAMIMADAM&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;MADAMIMADAM&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;ALRCAGOEUAOEURGCOEUOOIGFA&quot;</pre></td></tr></table></td></tr><tr><td><pre>Returns: &quot;AFLRCAGIOEOUAEOCEGRURGECOEAUOEOIGACRLFA&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)