Finals of the 16th Beijing Normal University Programming Contest - Repetition - G - Naming Norms

Topic description 

CamelCase is a specification for naming variable names, roughly using mixed uppercase and lowercase letters to form variable names. In this question, you can assume the following specific rules:

1. Each variable name is composed of at least 2 words, and the length of each word is at least 2;

2. The first letter of each word must be capitalized, and other positions must be lowercase (except for the first word of the variable name, all lowercase is allowed).

 

However, SK's English is very poor, and it is difficult to figure out which words are composed of long variable names, so it is very troublesome to look at the code of camel case nomenclature.

There is also an underscore nomenclature, the rules are relatively simple, that is, each word is connected with an underscore '_', and the letters are all lowercase.

Now give you some variable names, can you convert the variables that conform to the camel case convention to underscore?

Enter description:

The first line is a positive integer T (≤ 20000), indicating the number of groups of test data,
Each set of test data has only one row, including a variable name that only contains upper and lower case English letters and the length does not exceed 20.
Ensure that the total length of all test data variable names does not exceed 200,000.

Output description:

For each set of test data, output a line containing a string, if the variable name conforms to the camel case notation, it will be changed to underscore notation, otherwise unchanged.
Example 1

enter

10
mystring
myString
String
SS
my
mySString
mString
STRING
StrinG
IndexOfString

output

mystring
my_string
String
SS
my
mySString
mString
STRING
StrinG
index_of_string

Pit point: The second letter cannot be an uppercase letter. . .

Ideas: The second and last English letters cannot be uppercase, there cannot be two consecutive uppercase letters, and they cannot be all lowercase letters, and the rest can be directly converted.

ACDAIMA :

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main() {
    int t;
    cin >> t;
    string s;
    while(t--)
    {
        cin >> s;
        bool flag = true;
        int index = 0, ans = 1, len = s.length();
        for(int i = 1; i < len; i ++)
        {
            //第二个和最后一个英文字母不能是大写,不能有两个连续的大写字母,不能全是小写字母
            if(s[i] >= 'A' && s[i] <= 'Z' && i == index + 1) 
            {
                flag = false;
                break;
            }
            else if(s[i] >= 'A' && s[i] <= 'Z') index = i, ans++;
        }
        if(ans == 1||!flag || (index == len-1)) cout << s << endl;
        else
        {
            for(int i = 0; i < len; i ++)
            {
                if(s[i] >= 'A' && s[i] <= 'Z' && i != 0)
                {
                    printf("_%c",s[i]+32);
                }
                else if(s[i] >= 'A' && s[i] <= 'Z' && i == 0)
                {
                    printf("%c",s[i]+32);
                }
                else cout << s[i];
            }
            cout << endl;
        }
    }
    return 0;
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325960734&siteId=291194637