zoukankan      html  css  js  c++  java
  • Palindrome Names

    Palindrome Names

     Kattis - names

    Anna and Bob are having a baby. They both enjoy the advantage of having palindrome names, meaning that their names are spelled the same way forwards and backwards. Wanting to be good parents, they decide to give their child a palindrome name too. The only problem is that they aren’t sure if the one they picked is a palindrome. If it turns out it isn’t a palindrome, they want to change it to a palindrome using as few changes as possible. The allowed changes are:

    • Change one letter of the name.

    • Add a letter to the end of the name.

    Help Bob and Anna find out how many changes they need to make to the name to make it a palindrome.

    Input

    Input is the name they have chosen.

    Output

    Output the number of changes they need to make.

    Limits

    • The length of the name is at least 11 and at most 100100 characters.

    • The name consists of only lowercase letters a–z.

    Sample Input 1Sample Output 1
    kaia
    
    1
    
    Sample Input 2Sample Output 2
    abcdefgded
    
    4

    可以有修改,往最后添字符的骚操作,所以直接暴力贪心就好了

    #include<bits/stdc++.h>
    using namespace std;
    string s;
    int main() {
        int ans=1<<30;
        cin>>s;
        for(int i=0;s[i];i++){
            int cnt=i,beg=i,ed=s.size()-1;
            while(beg<=ed){
                if(s[beg]!=s[ed])
                    cnt++;
                beg++;
                ed--;
            }
            ans=min(ans,cnt);
        }
        cout<<ans<<endl;
    return 0;
    }
    大佬您太强了,还请多多指教哎
  • 相关阅读:
    Python Web学习笔记之Python多线程基础
    Python入门之python可变对象与不可变对象
    Python Web学习笔记之SOCK_STREAM和SOCK_DGRAM
    background和background-position相关笔记
    自定义switch开关
    获取浏览器类型和版本号
    随机生成字符串
    white-space详解
    文件选择按钮随笔
    mouse的各种事件
  • 原文地址:https://www.cnblogs.com/BobHuang/p/7203200.html
Copyright © 2011-2022 走看看