1 solutions

  • 0
    @ 2026-10-22 18:50:09

    C :

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<string.h>
    int ispalindrome(char s[], int start, int end) {
    	while (start < end) {
    		if (s[start] != s[end]) {
    			return 0;
    		}
    		start++;
    		end--;
    	}
    	return 1;
    }
    
    int main() {
    	char s[100];
    	scanf("%s", s);
    	int len = strlen(s);
    	int count = 0;
    	int i = 0;
    	while (i < len) {
    		int maxend = i;
    		for (int j = i; j < len; j++) {
    			if (ispalindrome(s, i, j)) {
    				maxend = j;
    			}
    		}
    		count++;
    		i = maxend + 1;
    	}
    	printf("%d", count - 1);
    	return 0;
    }
    

    C++ :

    undefined
    

    Python :

    # coding=utf-8
    # 判断子串是否为回文
    def is_palindrome(s, start, end):
        while start < end:
            if s[start] != s[end]:
                return False
            start += 1
            end -= 1
        return True
    
    # 计算最少调用次数
    def min_call(s):
        count = 0
        n = len(s)
        i = 0
        while i < n:
            # 从当前位置找最长的回文子串的结束位置
            max_j = i
            for j in range(i, n):
                if is_palindrome(s, i, j):
                    max_j = j
            count += 1  # 找到一个回文子串,计数+1
            i = max_j + 1  # 移动到下一个位置
        return count - 1  # 调用次数 = 回文子串数 - 1
    
    # 读取输入并输出结果
    s = input().strip()
    print(min_call(s))
    
    • 1

    Information

    ID
    401
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By