in c++
생성일: 2026년 5월 16일
답변에 사용된 모델 GPT-5.5 Thinking by Chat01
생성일: 2026년 5월 16일
답변에 사용된 모델 GPT-5.5 Thinking by Chat01
in c++
Paste this inside getMinimumTime:
cppint getMinimumTime(int sameTime, int partitionTime, string s) { int n = s.size(); const long long INF = 4e18; vector<long long> dp(n + 1, INF); dp[0] = 0; for (int i = 1; i <= n; i++) { vector<int> freq(256, 0); long long pairs = 0; // Try every last partition: s[j ... i-1] for (int j = i - 1; j >= 0; j--) { unsigned char ch = s[j]; // Adding this character creates pairs with same characters already inside pairs += freq[ch]; freq[ch]++; long long cost = pairs * sameTime; if (j > 0) { cost += partitionTime; // cut before this partition } dp[i] = min(dp[i], dp[j] + cost); } } return (int)dp[n]; }
Idea: dp[i] is the minimum time for the first i characters.
For every possible last substring, we count how many equal-character pairs it contains and choose the cheapest split.
in c++
Paste this inside the function:
cpplong getTotalPalindromeTransformationCost(string dna) { int n = dna.size(); vector<long long> cnt0(26, 1), cnt1(26, 0); vector<int> parity(26, 0); for (char ch : dna) { int x = ch - 'a'; parity[x] ^= 1; for (int i = 0; i < 26; i++) { if (parity[i]) cnt1[i]++; else cnt0[i]++; } } long long totalOddFreq = 0; for (int i = 0; i < 26; i++) { totalOddFreq += cnt0[i] * cnt1[i]; } long long evenPrefix = n / 2 + 1; long long oddPrefix = (n + 1) / 2; long long oddLengthSubstrings = evenPrefix * oddPrefix; long long answer = (totalOddFreq - oddLengthSubstrings) / 2; return answer; }
For any substring, only character counts with odd frequency matter.
If a substring has k odd-frequency characters, its minimum transformation cost is:
cppk / 2
The code sums this efficiently for all substrings in O(26 * n), which works for n <= 2 * 10^5.