USACO Section 1.3: Calf Flac

这题做了很久,一开始用了dp的方法,结果超时,后来用了现在的法子,还有一些细节需要注意,一个是string里不都是字母,另一个是输出的时候要在原来的字符串那里换行,这里用了set来记录换行位置,可能有更好的方法

  1 /*

  2 ID: leetcod3

  3 PROG: calfflac

  4 LANG: C++

  5 */

  6 #include <iostream>

  7 #include <fstream>

  8 #include <string>

  9 #include <map>

 10 #include <vector>

 11 #include <set>

 12 #include <algorithm>

 13 #include <stdio.h>

 14 #include <queue>

 15 #include <cstring>

 16 #include <cmath>

 17 #include <list>

 18 #include <cstdio>

 19 #include <cstdlib>

 20 #include <limits>

 21 #include <stack>

 22 

 23 using namespace std;

 24 

 25 ofstream fout ("calfflac.out");

 26 ifstream fin ("calfflac.in");

 27 

 28 int main() {

 29     string s1, s;

 30     set<int> newline;

 31     while (getline(fin, s1)) {

 32         s += s1;

 33         newline.insert(s.size());

 34     }

 35     s1 = "";

 36     int len = 1;

 37     int l, r;

 38     //cout << s << endl;

 39     for (int i = 0; i < s.size(); i++) {

 40         int left, right;

 41         left = right = i;

 42         int lentmp = -1;

 43         while (left >= 0 && right < s.size()) {

 44             if (!isalpha(s[left])) {

 45                 left--;

 46                 continue;

 47             }

 48             if (!isalpha(s[right])) {

 49                 right++;

 50                 continue;

 51             }

 52             if (tolower(s[left]) == tolower(s[right])) {

 53                 left--;

 54                 right++;

 55                 lentmp += 2;

 56             }

 57             else break;

 58         }

 59         left++, right--;

 60         while (left < s.size() && !isalpha(s[left])) left++;

 61         while (right >= 0 && !isalpha(s[right])) right--;

 62         if (lentmp > len) {

 63             len = lentmp;

 64             l = left;

 65             r = right;

 66         }

 67         left = i;

 68         right = i + 1;

 69         lentmp = 0;

 70         while (left >= 0 && right < s.size()) {

 71             if (!isalpha(s[left])) {

 72                 left--;

 73                 continue;

 74             }

 75             if (!isalpha(s[right])) {

 76                 right++;

 77                 continue;

 78             }

 79             if (tolower(s[left]) == tolower(s[right])) {

 80                 left--;

 81                 right++;

 82                 lentmp += 2;

 83             }

 84             else break;

 85         }

 86         left++, right--;

 87         while (left < s.size() && !isalpha(s[left])) left++;

 88         while (right >= 0 && !isalpha(s[right])) right--;

 89         if (lentmp > len) {

 90             len = lentmp;

 91             l = left;

 92             r = right;

 93         }

 94     }

 95     fout << len << endl;

 96     //cout << l << " " << r << endl;

 97     for (int i = l; i <= r; i++) {

 98         if (newline.find(i) != newline.end()) fout << endl;

 99         fout << s[i];

100     }

101     fout << endl;

102     return 0;

103 }

 

你可能感兴趣的:(USACO)