Algorithm

[๋ฐฑ์ค€ 10988] ํŒฐ๋ฆฐ๋“œ๋กฌ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

osean 2023. 3. 29. 20:36

๋ฌธ์ œ

 

10988๋ฒˆ: ํŒฐ๋ฆฐ๋“œ๋กฌ์ธ์ง€ ํ™•์ธํ•˜๊ธฐ

์ฒซ์งธ ์ค„์— ๋‹จ์–ด๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 100๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์œผ๋ฉฐ, ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

www.acmicpc.net

ํ’€์ด

  • ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด์„ ๋ฐ˜์œผ๋กœ ๋‚˜๋ˆ„๊ณ , ์•ž ๋ถ€๋ถ„๊ณผ ์ด๋ฅผ ๋’ค์ง‘์€ ๊ณณ์— ์œ„์น˜ํ•œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•
#include <bits/stdc++.h>
using namespace std;

int solve(string str) {
  int center = (str.length() / 2) + (str.length() % 2);

  // ์ค‘๊ฐ„์˜ ์ „๋ฉด๊ณผ ํ›„๋ฉด์„ ๋น„๊ต
  for (int i = 0; i < center; i++) {
    int j = str.length() - i - 1;
    if (str[i] != str[j]) return 0;
  }
  return 1;
}

string str;
int main() {
  cin >> str;
  cout << solve(str);
}
  • ์ž…๋ ฅํ•œ ๋ฌธ์ž์—ด๊ณผ ํ•ด๋‹น ๋ฌธ์ž์—ด์„ ๋ฐ˜์ „ํ•œ ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•
#include <bits/stdc++.h>
using namespace std;

int solve(string str) {
  string tmp = str;
  reverse(tmp.begin(), tmp.end());
  if (str == tmp) return 1;
  return 0;
}

string str;
int main() {
  cin >> str;
  cout << solve(str);
}