2108.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include <iostream>
  2. #include <cstdint>
  3. #include <vector>
  4. #include <string>
  5. using std::vector;
  6. using std::string;
  7. class Solution {
  8. public:
  9. string firstPalindrome(vector<string>& words) {
  10. auto len = words.size();
  11. auto i = len;
  12. for ( i = 0; i < len; ++i ) {
  13. string s = words[i];
  14. unsigned int slen = (unsigned int)s.length() - 1;
  15. unsigned int end = ((unsigned int) (slen / 2)) + 1;
  16. for ( unsigned int j = 0; j < end; ++j ) {
  17. if ( s[j] != s[slen-j] ) { goto outer; }
  18. }
  19. return s;
  20. outer: ;
  21. }
  22. return "";
  23. }
  24. };
  25. void r(vector<string>& w) {
  26. std::cout << "Solution().firstPalindrome([";
  27. for ( uint64_t i = 0; i < w.size(); ++i ) {
  28. if ( i == 0 ) { std::cout << "\"" << w[i] << "\""; continue; }
  29. std::cout << ", \"" << w[i] << "\"";
  30. }
  31. std::cout << "]) = \"" << Solution().firstPalindrome(w) << "\"";
  32. };
  33. int main(void) {
  34. vector<string> w1 = {"abc", "car", "ada", "racecar", "cool"};
  35. r(w1);
  36. return 0;
  37. }