Bb: statistical palindrome substring (basic computer test questions)

Time:2022-7-30

Now I’ll give you a string s. please calculate how many consecutive substrings in s are palindromes.
input
The input contains multiple sets of test data. Each set of input is a non empty string with a length of no more than 5000.
output
For each group of inputs, the number of palindrome substrings is output.
Sample input copy
aba
aa
Sample output copy
4
3

Code example (written by myself, can only pass the platform, imperfect)

//#include<iostream>
//#include<string>
//#include<string.h>
//using namespace std;
//
//int main(){
//    string s;
//    while(getline(cin,s)){
//        int len = s.length();
//        int i,left = 0,right = 0;
//        int cnt = len;
//        for(i = 0; i < len; i++){
//            left = i - 1;// Compare two symmetrical characters separated by one character 
//            right = i + 1;
//            while(left >= 0 && right < len){
//If (s[left] = = s[right]) {// compare it symmetrically with one character apart (xYx type, compare two XX) to see whether it is equal. If it is equal, add 1 to the palindrome substring 
//                    ++ cnt;
//                    -- left;
//                    ++ right;// If it matches, it will spread to both sides to see whether the two A's on both sides of the longer substring (separated by three Y's, axyxa) are equal, until it exceeds the boundary, and if it is equal, it will be added by 1 
//                }else{
//                    break;
//                }
//            }
//            left = i - 1;// Compare the two adjacent characters on the left and right 
//            right = i;
//            while(left >= 0 && right < len){
//If (s[left] = = s[right]) {// compare with adjacent characters (type XX, compare two XX) to see whether they are equal. If they are equal, add 1 to the palindrome substring  
//                    ++ cnt;
//                    -- left;
//                    ++ right;// As above, if it conforms to spread to both sides (axxa), check whether the two sides a are equal until the whole string is traversed 
//                }else{
//                    break;
//                }
//            }
//        }
//        cout << cnt << endl;
//    }
//    return 0;
//}

Bb: statistical palindrome substring (basic computer test questions)

Recommended Today

Digital DP-902. Number combinations up to N

Problem Description Given an array digits of digits in non-decreasing order. A number you can write with digits[i] any number of times. For example, if digits = ['1','3','5'], we can write digits like '13', '551', and '1351315'. Returns the number of positive integers that can be generated that are less than or equal to the […]