The classic greedy algorithm. The characteristic of greedy algorithm is always take the optimal solution, and finally get the best, this point is different from the dynamic one. Both the dynamic and the search are multi-strategy, so the greedy understanding of the difficulty is still very small.
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #include < queue> #include <cmath> #include <list>using namespace Std;int n,a;vector<char> ans;int main () { scanf ("%d", &n); Char s[2500]; Ans.clear (); char C = GetChar (); for (int i=0;i<n;i++) {char c = getchar (); S[i] = c; c = GetChar (); } S[n] = ' + '; int star = 0,ENDD = N-1; while (STAR<=ENDD) {for (int i=star;i<=endd;i++) {if (endd-(I-star) <i| | I==ENDD) {ans.push_back (S[star]); star++; Break } if (s[i]==s[endd-(I-star)]) continue; else if (s[i]<s[endd-(I-star)]) {Ans.push_back (S[star]); star++; Break } else if (s[i]>s[endd-(I-star)]) {Ans.push_back (s[endd]); endd--; Break }}} for (int i=0;i<ans.size (); i++) {printf ("%c", Ans[i]); if ((i+1)%80==0) printf ("\ n"); } int t = Ans.size (); if (t%80!=0) printf ("\ n"); return 0;}
Best Cow Line (POJ-3617)