Find a new type character of the specified type

Find a new type character of the specified type

Title description

The definition of the new type character is as follows:
1. The new type character is a character string with a length of 1 or 2.

  1. The expression can only be lowercase letters, for example, "e"; it can also be uppercase letters + lowercase letters, for example, "Ab"; it can also be uppercase letters + uppercase letters, for example, "DC".

Now given a string str, str must be the result of the correct combination of several new types of characters. For example, "eaCCBi" is composed of new type characters "e", "a", "CC" and "Bi". Given an integer k, which represents the position in str. Please return the new type character at the kth position.

Enter a description:

The input contains two lines. The first line contains two integers n, k (1 ≤ n ≤ 1 0 5, 0 ≤ k ≤ n − 1) n,k(1 \leq n \leq 10^5,0 \leq k \ leq n-1)n,k(1n105,0kn1 ) , n represents the length of the string str, and the second line contains a string representing the string str.

Output description:

The output contains a new type of character specified by the k position.

Example 1
enter
11 7
aaABCDEcBCg
Output
Ec

answer:

String simulation. The basic solution is to traverse from front to back, and know what character it is when you reach position k. There is another novel approach, starting from position k-1 and traversing to the left, stopping at the first lowercase letter, and counting the number of uppercase letters num:

  • If num is an odd number, the new type character must be str[k-1,k];
  • If str[k] is a capital letter, the new type of character is str[k,k+1];
  • Otherwise, the new type character is str[k].

Note: The k in the title starts from 0.

Code:
#include <cstdio>
#include <cstring>

using namespace std;

const int N = 100010;

char s[N];
int n, k;

int main(void) {
    
    
    scanf("%d%d", &n, &k);
    scanf("%s", s);
    int ans = 0;
    for (int i = k - 1; i >= 0; --i) {
    
    
        if (s[i] >= 'a' && s[i] <= 'z') break;
        ++ans;
    }
    if (ans & 1) printf("%c%c\n", s[k - 1], s[k]);
    else if (s[k] >= 'A' && s[k] <= 'Z') printf("%c%c\n", s[k], s[k + 1]);
    else printf("%c\n", s[k]);
    return 0;
}

Guess you like

Origin blog.csdn.net/MIC10086/article/details/108961760