목록팰린드롬 (7)
넘치게 채우기
https://www.acmicpc.net/problem/1509BOJ - 팰린드롬 분할문제 유형: 팰린드롬, 문자열 처리, 다이나믹 프로그래밍문제 난이도: Gold I시간 제한: 2초메모리 제한: 128MB 문제세준이는 어떤 문자열을 팰린드롬으로 분할하려고 한다. 예를 들어, ABACABA를 팰린드롬으로 분할하면, {A, B, A, C, A, B, A}, {A, BACAB, A}, {ABA, C, ABA}, {ABACABA}등이 있다.분할의 개수의 최솟값을 출력하는 프로그램을 작성하시오. 입력첫째 줄에 문자열이 주어진다. 이 문자열은 알파벳 대문자로만 이루어져 있고, 최대 길이는 2,500이다. 출력첫째 줄에 팰린드롬 분할의 개수의 최솟값을 출력한다. 풀이isPalindrome[l][r] = [l, r..
https://leetcode.com/problems/construct-k-palindrome-strings/description/leetcode - Construct K Palindrome Strings문제 유형: 문자열 처리, 팰린드롬문제 난이도: Medium 문제Given a string s and an integer k, return true if you can use all the characters in s to construct k palindrome strings or false otherwise. 문자열 s와 정수 k가 주어집니다. 만약 s의 글자들로 k개의 팰린드롬 문자열로 만들 수 있으면 true를 아니면 false를 반환하시오. 풀이꽤 트리키해 보일 수 있지만, 문제를 간단하게 만들..
https://www.acmicpc.net/problem/10942BOJ - 팰린드롬?문제 유형: 팰린드롬, 다이나믹 프로그래밍, 투 포인터문제 난이도: Gold IV시간 제한: 0.5초메모리 제한: 256MB 문제명우는 홍준이와 함께 팰린드롬 놀이를 해보려고 한다.먼저, 홍준이는 자연수 N개를 칠판에 적는다. 그 다음, 명우에게 질문을 총 M번 한다.각 질문은 두 정수 S와 E(1 ≤ S ≤ E ≤ N)로 나타낼 수 있으며, S번째 수부터 E번째 까지 수가 팰린드롬을 이루는지를 물어보며, 명우는 각 질문에 대해 팰린드롬이다 또는 아니다를 말해야 한다.예를 들어, 홍준이가 칠판에 적은 수가 1, 2, 1, 3, 1, 2, 1라고 하자.S = 1, E = 3인 경우 1, 2, 1은 팰린드롬이다.S = 2..
https://leetcode.com/problems/shortest-palindrome/description/?envType=daily-question&envId=2024-09-20leetcode - Shortest Palindrome문제 유형 : 팰린드롬, 문자열 처리, KMP 알고리즘문제 난이도 : Hard 문제You are given a string s. You can convert s to a palindrome by adding characters in front of it.Return the shortest palindrome you can find by performing this transformation. 문자열 s를 받는다. 당신은 s에 접두사를 붙여서 변환시킬 수 있다.s에 문자를 ..
설명Naive 패턴 검색에서는, 우리는 모든 pattern의 길이와 같은 부분문자열들을 원본 문자열에서 추출해서 각각 한 글자씩 비교했다. Rabin-Karp 알고리즘에서도, 모든 부분문자열을 검사하지만, 대신 패턴의 해시 값와 현재 부분문자열의 해시 값을 비교한다.그리고 해시 값이 매칭되면, 패턴이 맞는 것이다.Rabin-Karp 알고리즘은 다음의 해시 값들을 계산할 필요가 있다:패턴의 해시 값더미 문자열(haystack)에서의 길이가 m인 모든 부분문자열해시 값은 문자열에서 패턴을 찾는 데 효율적으로 사용된다.해시 값은 rolling hash function으로 계산되는데, 기존의 글자를 하나 버리고 새 글자를 가져오면서 해시 값을 업데이트하면 된다.롤링 해시 함수의 과정은 다음과 같다:기저 수 b와..
https://leetcode.com/problems/find-the-closest-palindrome/description/?envType=daily-question&envId=2024-08-24leetcode - Find the Closest Palindrome문제 유형 : 팰린드롬, 회문, 문자열 처리문제 난이도 : Hard 문제Given a string n representing an integer, return the closest integer (not including itself), which is a palindrome. If there is a tie, return the smaller one.The closest is defined as the absolute difference mi..
https://leetcode.com/problems/longest-palindromic-substring/ = 0 && right < s.size() && s[left] == s[right]) { left--; right++; } return right - left - 1; } string longestPalindrome(string s) { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); const int n = s.size(); int maxLen = 0; int start = 0; for (int i = 0; i < n; i++) { int len1 = expandAroundCenter(s, i, i); int len2 = expandAroundC..