Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Judge: https://zerojudge.tw/ShowProblem?problemid=c462
- #include <bits/stdc++.h>
- using namespace std;
- int nums[100000] = {0};
- int bound = 0;
- // My idea is to turn the string to several numbers, such as
- // 'ABabaA' => [2,3,1]
- // 'IJjiHW' => [2,2,2]
- // 'dwFwGe' => [2,1,1,1,1]
- // 'ojojowP' => [6,1]
- // Got the pattern? The numbers show the amount of consecutive letters in the same case (both in lower or upper)
- //
- // Now the problem would be:
- // find the maximum length in these nums satisfy the conditions:
- // (nums[l] >= k, nums[r] >= k, nums[mid] == k, l < mid < r)
- // Let us call the nums[l] 'head', nums[mid] 'body', and nums[r] 'tail'.
- int main(){
- // the variable k we declare
- int k;
- cin >> k;
- // the input string
- string s;
- cin >> s;
- // the variable 'cnt' represent the current number of same-case-letters
- int cnt = 0;
- // Remember, string's indexes start with 0, not 1
- for(int i = 0; i < s.size(); ++i){
- // s[i] is the current letter, and s[i-1] is the previous letter
- if(i == 0){
- // the first one, of course cnt is 1
- cnt = 1;
- }else if('a' <= s[i] && s[i] <= 'z'){
- if('a' <= s[i-1] && s[i-1] <= 'z'){
- // both s[i] and s[i-1] are lowercase letters
- cnt += 1;
- }else if('A' <= s[i-1] && s[i-1] <= 'Z'){
- // s[i] and s[i-1] are in different case
- nums[bound++] = cnt;
- cnt = 1;
- }
- }else if('A' <= s[i] && s[i] <= 'Z'){
- if('a' <= s[i-1] && s[i-1] <= 'z'){
- // s[i] and s[i-1] are in different case
- nums[bound++] = cnt;
- cnt = 1;
- }else if('A' <= s[i-1] && s[i-1] <= 'Z'){
- // both s[i] and s[i-1] are uppercase letters
- cnt += 1;
- }
- }
- if(i == s.size()-1){
- // This step is important!!!
- // At last one, you may miss to add cnt to nums[]
- nums[bound++] = cnt;
- }
- }
- // Here we have to find consecutive numbers as many as possible
- // Remember, the head and the tail of the sequence could be more than k letters
- int tail = 0; // 1 represents the current num could be the tail but not for body
- int ans = 0; // the current best answer
- int cur = 0; // the current answer
- for(int i = 0; i < bound; ++i){
- if(nums[i] == k){ // body
- if(tail == 1){
- cur = 2;
- }else{
- cur += 1;
- }
- tail = 0;
- }else if(nums[i] > k){ // head or tail
- if(cur == 0){ // no body or tail, so let nums[i] be head would be better
- cur += 1;
- }else if(tail == 1){ // previous tail be head, nums[i] be tail
- cur = 2;
- }else{ //
- cur += 1;
- }
- tail = 1;
- }else{ // nums[i] cannot be part of answer
- cur = 0;
- tail = 0;
- }
- // maintain the best answer
- ans = max(ans, cur);
- }
- cout << ans * k << '\n';
- return 0;
- }
- // It may seems that I wrote a LONG code, however I wrote lots of comments too.
- // You may delete all these comments and see how short the code is :-)
RAW Paste Data