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;
    }
  • 相关阅读:
    云计算架构
    Java多线程中static变量的使用
    Spring单例与线程安全小结
    sparkSQL实战详解
    sparkSQL整体实现框架
    spark架构
    如何快速定位出一个IP地址的归属地?——二分查找变体
    如何在 1000 万个整数中快速查找某个整数?——二分查找
    语言库中常用的排序算法qsort()底层结构
    链表常考笔试面试题(常备)
  • 原文地址:https://www.cnblogs.com/zhien-aa/p/6285871.html
Copyright © 2011-2022 走看看