zoukankan      html  css  js  c++  java
  • Kattis

    Peragrams

    /problems/peragrams/file/statement/en/img-0001.jpg
    Photo by Ross Beresford

    Per recently learned about palindromes. Now he wants to tell us about it and also has more awesome scientific news to share with us.

    “A palindrome is a word that is the same no matter whether you read it backward or forward”, Per recently said in an interview. He continued: “For example, add is not a palindrome, because reading it backwards gives dda and it’s actually not the same thing, you see. However, if we reorder the letters of the word, we can actually get a palindrome. Hence, we say that add is a Peragram, because it is an anagram of a palindrome”.

    Per gives us a more formal definition of Peragrams: “Like I said, if a word is an anagram of at least one palindrome, we call it a Peragram. And recall that an anagram of a word ww contains exactly the same letters as ww, possibly in a different order.”

    Task

    Given a string, find the minimum number of letters you have to remove from it, so that the string becomes a Peragram.

    Input

    Input consists of a string on a single line. The string will contain at least 11 and at most 10001000 characters. The string will only contain lowercase letters a-z.

    Output

    Output should consist of a single integer on a single line, the minimum number of characters that have to be removed from the string to make it a Peragram.

    Sample Input 1Sample Output 1
    abc
    
    2
    
    Sample Input 2Sample Output 2
    aab
    
    0

    题意

    问给出的字符串需要删除多少个字母后才能组成回文串

    思路

    相同字母个数是偶数的话才能构成回文,奇数字母只能有一个,所以只需要统计每个字母的个数,然后,答案就是个数为奇数的字母数-1

    代码

    #include<bits/stdc++.h>
    int vis[30];
    char aa[1005];
    int main() {
        while (~scanf("%s", aa))
        {
            memset(vis, 0, sizeof(vis));
            int len = strlen(aa);
            for (int i = 0; i < len; i++)
            {
                vis[aa[i] - 'a']++;
            }
            int ans = 0;
            for (int i = 0; i < 26; i++)
            {
                if (vis[i] % 2)
                {
                    ans++;
                }
            }
            if (ans) ans--;
            printf("%d
    ", ans);
    
        }
        return 0;
    }
  • 相关阅读:
    subString源码分析
    我的three.js学习记录(三)
    我的three.js学习记录(二)
    2017-10-15
    我的three.js学习记录(一)
    我的Spring学习记录(三)
    我的Spring学习记录(二)
    我的Hibernate学习记录(二)
    我的Hibernate学习记录(一)
    Tomcat学习笔记
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6285871.html
Copyright © 2011-2022 走看看