All Palindrome

链接:https://ac.nowcoder.com/acm/contest/624/A
来源:牛客网
 

Ramen likes palindrome.

A palindrome is a word, number, phrase, or other sequences of characters which reads the same backward as forward, such as madam or racecar or the number 10801.

An all palindrome is such a string that each substring of it is a palindrome. For example, the string aaa is an all palindrome while aba is not.

There is a string, and Ramen wants to make it an all palindrome string. He has a magic skill that he can remove any character of the string as much as he wants. But cast such a spell will cost him a lot of energy. As a lazy boy, he wants to save energy to play games, so can you tell him the minimum number of the necessary characters that he needs to remove?
 

输入描述:

The input contains precisely two lines.

The first line is an integer n(1 <= n <= 100000), which indicates the length of the given string.

The second line is the given string S. It's guaranteed that S contains only lower case Lattin letters, i.e., a-z.

输出描述:

For each test case, output the minimum number of necessary removals.

示例1

输入

复制

22
welcometotheupcofhitwh

输出

复制

19

示例2

输入

复制

20
itstheeasiestproblem

输出

复制

16

示例3

输入

复制

25
goandgrabyoursignupreward

输出

复制

21

思路:炸一眼以为是回文串更改判断之后发现水体直接输出桶排最大的

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int book[100009];
int main()
{
    int n;string a;
    scanf("%d",&n);
    cin>>a;int max1 = 0;
    for(int i = 0; i < n;++i)
    {
        book[a[i]-'a']++;
        if(book[a[i]-'a']>max1)
        {
            max1 = book[a[i]-'a'];
        }
    }
     
    printf("%d\n",n-max1);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_43568078/article/details/89298272
ALL