CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home  » November 2009 (Contest X) » Magic Strings » All Submissions » David Nehme [130247]
0
Your rating: None

CodeChef submission 130247 (C++ 4.0.0-8)

CodeChef submission 130247 (C++ 4.0.0-8) plaintext list. Status: WA, problem J5, contest NOV09. By abremod (abremod), 2009-11-11 04:04:10.
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. using namespace std;
  5.  
  6.  
  7.  
  8. class SymmetricString {
  9.  
  10. protected:
  11. virtual bool lex_less_than(const SymmetricString& rhs) const = 0;
  12. const int length_;
  13. public:
  14. SymmetricString(int length) : length_(length) {}
  15. inline int length() const {return length_;}
  16.  
  17. inline bool shorter_than(const SymmetricString& rhs) const {
  18. return length_ < rhs.length_;
  19. }
  20.  
  21. inline bool operator<(const SymmetricString& rhs) const {
  22. if (shorter_than(rhs)) return true;
  23. if (!shorter_than(rhs)) return false;
  24. return lex_less_than(rhs);
  25. }
  26.  
  27. virtual void print(ostream& os) const=0;
  28. friend ostream& operator<<(ostream& os, const SymmetricString& rhs);
  29. };
  30.  
  31.  
  32. ostream& operator<<(ostream& os, const SymmetricString& rhs) {
  33. rhs.print(os);
  34. return os;
  35. }
  36.  
  37.  
  38. class EmptyString : public SymmetricString {
  39. public:
  40. EmptyString() : SymmetricString(0) {}
  41. bool operator<(const SymmetricString& rhs) const { return shorter_than(rhs);}
  42.  
  43. void print (ostream& os) const {}
  44.  
  45. // guaranteed to be the same subclass
  46. virtual bool lex_less_than(const SymmetricString& rhs) const {
  47. return false;
  48. }
  49.  
  50. };
  51.  
  52. class SingleChar : public SymmetricString {
  53. const char x_;
  54. public:
  55. SingleChar(char x) : SymmetricString(1), x_(x) {}
  56.  
  57. void print(ostream& os) const {
  58. os << x_;
  59. }
  60.  
  61.  
  62. // guaranteed to be the same subclass
  63. virtual bool lex_less_than(const SymmetricString& rhs) const {
  64. return x_ < static_cast<const SingleChar&>(rhs).x_;
  65. }
  66. };
  67.  
  68.  
  69. class FullSymmetricString : public SymmetricString {
  70. const char first_;
  71. const SymmetricString* middle_;
  72. const char last_;
  73. public:
  74. FullSymmetricString(char first, const SymmetricString* middle, char last) :
  75. SymmetricString(middle->length() + 2), first_(first), middle_(middle), last_(last)
  76. {}
  77.  
  78. void print(ostream& os) const {
  79. os << first_;
  80. os << *middle_;
  81. os << last_;
  82. };
  83.  
  84. virtual int length() const {return length_;}
  85. // guaranteed to be the same subclass
  86. virtual bool lex_less_than(const SymmetricString& rhs) const {
  87. const FullSymmetricString& casted_rhs=static_cast<const FullSymmetricString&>(rhs);
  88. if (first_ < casted_rhs.first_) return true;
  89. if (first_ > casted_rhs.first_) return false;
  90. if (*(this->middle_) < *(casted_rhs.middle_)) return true;
  91. if ( *(casted_rhs.middle_) < *(this->middle_)) return false;
  92. return last_ < casted_rhs.last_;
  93. }
  94. };
  95.  
  96.  
  97.  
  98. class Instance
  99. {
  100. string full_sequence_;
  101. EmptyString* empty_string_;
  102. // typedef enum {unknown = 0, take_both, take_left, take_right} optimal_decision;
  103. vector<vector<const SymmetricString*> > calculated_best_left_handed_; // always > 0 if computed
  104. vector<vector<const SymmetricString*> > calculated_best_right_handed_;
  105.  
  106. int max(int x1, int x2, int x3) {
  107. if (x1 > x2 && x2 >= x3) return x1;
  108. if (x1 > x3 && x3 >= x2) return x1;
  109. if (x2 > x3) return x2;
  110. return x3;
  111. }
  112.  
  113.  
  114. const SymmetricString* longest(const SymmetricString* x1, const SymmetricString* x2) {
  115. if (*x2 < *x1) return x1;
  116. return x2;
  117. }
  118.  
  119. const SymmetricString* longest3(const SymmetricString* x1, const SymmetricString* x2, const SymmetricString* x3) {
  120. return longest(longest(x1, x2), x3);
  121. }
  122.  
  123. const SymmetricString* longest(const SymmetricString* x1, const SymmetricString* x2, const SymmetricString* x3) {
  124. if (x1->length() > x2->length()) return longest(x1, x3);
  125. if (x2->length() > x1->length()) return longest(x2, x3);
  126. // x1 and x2 have the same length
  127. if (x3->length() > x1->length()) return x3;
  128. if (x1->length() > x3->length()) return longest(x1, x2);
  129.  
  130. // all 3 have the same length (possible optimization here)
  131. return longest3(x1, x2, x3);
  132.  
  133. }
  134.  
  135.  
  136. public:
  137. Instance(const string& a_string) : full_sequence_(a_string),
  138. empty_string_(new EmptyString()),
  139. calculated_best_left_handed_(a_string.length()),
  140. calculated_best_right_handed_(a_string.length()) {
  141. for (unsigned i = 0; i != a_string.length(); ++i) {
  142. calculated_best_left_handed_[i].resize(a_string.length());
  143. calculated_best_right_handed_[i].resize(a_string.length());
  144. calculated_best_left_handed_[i][i] = new SingleChar(full_sequence_[i]);
  145. calculated_best_right_handed_[i][i] = new SingleChar(full_sequence_[i]);
  146. }
  147. }
  148.  
  149.  
  150. const SymmetricString* longest_left_handed() {
  151. return longest_left_handed(0, full_sequence_.length()-1);
  152. }
  153.  
  154.  
  155. const SymmetricString* longest_left_handed(int i, int j) {
  156.  
  157. if (i > j) return empty_string_;
  158. if (calculated_best_left_handed_[i][j]) {
  159. return calculated_best_left_handed_[i][j];
  160. }
  161. const SymmetricString* best_take_both = empty_string_;
  162. if (full_sequence_[i] < full_sequence_[j]) {
  163. best_take_both = new FullSymmetricString(full_sequence_[i], longest_right_handed(i+1, j-1), full_sequence_[j]);
  164. }
  165. const SymmetricString* best_skip_left = longest_left_handed(i+1, j);
  166. const SymmetricString* best_skip_right = longest_left_handed(i, j - 1);
  167.  
  168. calculated_best_left_handed_[i][j] = longest(best_take_both, best_skip_left, best_skip_right);
  169. if (calculated_best_left_handed_[i][j] != best_take_both && best_take_both != empty_string_) {
  170. delete best_take_both;
  171. }
  172. return calculated_best_left_handed_[i][j];
  173. }
  174.  
  175. const SymmetricString* longest_right_handed(int i, int j) {
  176. if (i > j) return empty_string_;
  177.  
  178. if (calculated_best_right_handed_[i][j]) {
  179. return calculated_best_right_handed_[i][j];
  180. }
  181. const SymmetricString* best_take_both = empty_string_;
  182. if (full_sequence_[i] > full_sequence_[j]) {
  183. best_take_both = new FullSymmetricString(full_sequence_[i], longest_left_handed(i + 1, j - 1), full_sequence_[j]);
  184. }
  185. const SymmetricString* best_skip_left = longest_right_handed(i+1, j);
  186. const SymmetricString* best_skip_right = longest_right_handed(i, j - 1);
  187. calculated_best_right_handed_[i][j] = longest(best_take_both, best_skip_left, best_skip_right);
  188. if (calculated_best_right_handed_[i][j] != best_take_both && best_take_both != empty_string_) {
  189. delete best_take_both;
  190. }
  191. return calculated_best_right_handed_[i][j];
  192. }
  193. };
  194.  
  195. int do_main() {
  196. int n_cases;
  197. string test_value;
  198. cin >> n_cases;
  199. for (int i = 0; i < n_cases; ++i) {
  200. cin >> test_value;
  201. Instance instance(test_value);
  202. cout << *(instance.longest_left_handed()) << '\n';
  203. }
  204. return 0;
  205. }
  206.  
  207.  
  208.  
  209.  
  210. void test_value(const string& a_string, int expected_value)
  211. {
  212. Instance an_instance(a_string);
  213. const SymmetricString* computed_value = an_instance.longest_left_handed();
  214. cout << a_string << ' ' << *computed_value << " " << computed_value->length() << " == " << expected_value << '\n';
  215. }
  216.  
  217.  
  218. int main() {
  219. test_value("a", 1);
  220. test_value("", 0);
  221. test_value("ab", 2);
  222. test_value("aa", 1);
  223. test_value("ba", 1);
  224. test_value("aba", 2);
  225. test_value("bab", 2);
  226. test_value("cba", 1);
  227. test_value("abc", 3);
  228.  
  229. test_value("difference", 10);
  230. test_value("similarq", 6);
  231. test_value("caaat", 3);
  232.  
  233. string random_string;
  234. for (int i = 0; i < 2000; ++i) {
  235. random_string.push_back('a'+(i %26));
  236. }
  237. test_value(random_string, 99);
  238. return 0;
  239. }


Comments

  • Login or Register to post a comment.

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com