백준 알고리즘 8892번 : 팰린드롬

Zoo Da·2021년 10월 14일
0

백준 알고리즘

목록 보기
227/337
post-thumbnail

링크

https://www.acmicpc.net/problem/8892

sol1) 구현

#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#include<bits/stdc++.h>
#define X first
#define Y second
#define pb push_back
#define fastio cin.tie(0)->sync_with_stdio(0)
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define sz(v) (int)(v).size()
#define all(v) v.begin(), v.end()
#define rall(v) (v).rbegin(), (v).rend()
#define compress(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define OOB(x, y) ((x) < 0 || (x) >= n || (y) < 0 || (y) >= m)
#define debug(x) cout << (#x) << ": " << (x) << '\n'
using namespace std;
using ll = long long;
using ull = unsigned long long;
using dbl = double;
using ldb = long double;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
using vi = vector<int>;
using vs = vector<string>;
using tii = tuple<int,int,int>;
template<typename T> using wector = vector<vector<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 7;
const ll LNF = 1e18 + 7;

bool isPal(string a, string b){
  string c = a + b,temp;
  temp = c;
  reverse(all(c));
  if(c == temp) return true;
  return false;
}

int main() {
	fastio;
  int tc; cin >> tc;
  while(tc--){
    int k; cin >> k;
    vs v;
    bool flag = false;
    for(int i = 0; i < k; i++){
      string a; cin >> a;
      v.pb(a);
    }
    string ans;
    for(int i = 0; i < k; i++){
      for(int j = 0; j < k; j++){
        if(i == j) continue;
        if(isPal(v[i],v[j])){
          cout << v[i] + v[j] << "\n";
          flag = true;
          break;
        }
      }
      if(flag) break;
    }
    if(!flag) cout << 0 << "\n";
  }  
  return 0;
}

문제에서 요구하는 바에 따라 구현해주면 됩니다.

profile
메모장 겸 블로그

0개의 댓글