10174 팰린드롬 python, c++

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

     

    10174번: 팰린드롬

    팰린드롬은 앞으로 읽으나 뒤로 읽으나 똑같은 단어나 숫자들을 말한다. 일반적으로 대소문자를 구분하지 않지만, 공백은 구분한다. 다음은 팰린드롬의 예시이다. Anna Harrah Arora Nat tan 9998999 123

    www.acmicpc.net

     

     

     

     

     

     

    문제 접근

    문자열을 다루는 문제.

    문자열을 대소문자 구분하지않고, 뒤집을때 똑같은지 판별하는 것을 구현하면 된다.

     

    문자열 뒤집기 python

     

    문자열 뒤집기 python

    파이썬에서 문자열을 뒤집는 방법에는 여러 가지가 있습니다. 다음은 세 가지 일반적인 방법입니다. 방법 1: 슬라이싱 사용 string = "Hello, World!" reversed_string = string[::-1] print(reversed_string) # Output: "!d

    portable-paper.tistory.com

    문자열 뒤집기 C++

     

    문자열 뒤집기 C++

    C++에서 문자열을 뒤집는 방법 2가지 방법 1: reverse()기능 사용 #include #include #include int main() { std::string str = "Hello, World!"; std::reverse(str.begin(), str.end()); std::cout

    portable-paper.tistory.com

     

     

    최대한 함수를 사용해서 구현했다.

     

     

     

     

     

    코드

    python

    import sys
    
    input = sys.stdin.readline
    
    def is_palindrome(s):
      s = s.lower()
      rs = ''.join(reversed(s))
      return rs==s
    
    n = int(input())
    for i in range(T):
      s = input().strip()
      if is_palindrome(s):
        print("Yes")
      else:
        print("No")

    압축가능하지만 그냥 쭉 썼다.

     

     

    31256kb 40ms

     

     

     

    C++

    #include <algorithm>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    /*
    
    */
    bool is_palindrome(string s) {
      for(int i=0; i<s.size(); i++){
        s[i] = tolower(s[i]);
      }
      string rs = s;
      reverse(rs.begin(), rs.end());
      return s == rs;
    }
    
    int main() {
      ios_base::sync_with_stdio(0);
      cin.tie(0);
      cout.tie(0);
    
      int n;
      cin >> n;
      cin.ignore();
      string s;
      for (int i = 0; i < n; i++) {
        getline(cin, s);
        cout << (is_palindrome(s) ? "Yes" : "No") << endl ;
      }
    }

     

     

    2024kb 0ms

     

    '백준 > 문제풀이_python,C++' 카테고리의 다른 글

    20040 사이클게임 (python)  (0) 2023.11.22
    1449 수리공 항승 python, C++  (0) 2023.04.19
    4803 트리 python, c++  (0) 2023.04.17
    25192 인사성 밝은 곰곰이  (0) 2023.04.16
    18917 수열과 쿼리 38 _python, c++  (0) 2023.04.12

    댓글