• Topcoder712B


    Problem Statement

         You have some cards. Each card contains a single lowercase letter. You are given these letters as the characters of the string card.

    A palindrome is a string that reads the same forwards and backwards. Examples of palindromes: "eve", "abba", "aaaaaa", and "racecar".

    Use the cards you have to spell some palindromes. In particular:
    • Each card must be used in exactly one of the palindromes.
    • The total number of palindromes must be as small as possible.
    Return a vector <string> containing the palindromes you built. (Each element of the return value should be one of the palindromes.)

    A solution always exists. If there are multiple optimal solutions, you may choose and output any one of them.

    Definition

        
    Class: MakePalindrome
    Method: constructMinimal
    Parameters: string
    Returns: vector <string>
    Method signature: vector <string> constructMinimal(string card)
    (be sure your method is public)

    Limits

        
    Time limit (s): 2.000
    Memory limit (MB): 256
    Stack limit (MB): 256

    Constraints

    - card will contain between 1 and 1,000 characters, inclusive.
    - Each character in card will be a lowercase English letter ('a'-'z').

    Examples

    0)  
        
    "abbaa"
    Returns: {"ababa" }
    We can rearrange all letters into a single palindrome. There are two ways to do so: one is "ababa", the other is "baaab".
    1)  
        
    "abc"
    Returns: {"a", "b", "c" }
    This time the only solution is to build three palindromes, each consisting of a single letter. Note that you may return the three strings in any order.
    2)  
        
    "aaabbbccc"
    Returns: {"aba", "bcb", "cac" }
    There are other solutions like {"aaa", "bbb", "ccc"}
    3)  
        
    "topcoder"
    Returns: {"oco", "d", "e", "p", "r", "t" }
     
    4)  
        
    "z"
    Returns: {"z" }
    public class MakePalindrome
    {
    vector  constructMinimal(string card) {
    	map<char, int> mp;
    	mp.clear();
    	vector ret;
    	ret.clear();
    	for(int i=0;i<card.size();i++) {
    		mp[card[i]] ++;
    	}
    	string f = "";
    	for(char i = 'a'; i <= 'z'; i++) {
    		// cout<<i<<" "<<mp[i]<<"
    ";
    		if(mp[i] % 2 == 0) {
    			for(int j=0;j<mp[i]/2;j++) {
    				f.push_back(i);
    			}
    			mp[i] = 0;
    		}
    	}
    	bool flag = 0;
    	// Add character with odd occurrence
    	for(char i = 'a'; i <= 'z'; i++) {
    		if(mp[i]&1) {
    			flag = 1;
    			for(int j=0;j<mp[i]/2;j++) {
    				f.push_back(i);
    			}
    			string temp = f;
    			cout<<temp<<"
    ";
    			reverse(temp.begin(), temp.end());
    			f = f + i + temp;
    			mp[i] = 0;
    			break;
    		}
    	}
    	// if all characters are of even occurrence
    	if(!flag){
    		string temp = f;
    		reverse(temp.begin(), temp.end());
    		f = f + temp;
    		ret.push_back(f);
    		return ret;
    	}
    
    	ret.push_back(f);
    	f = "";
    	// for all other characters of odd occurrence
    	for(char i = 'a'; i <= 'z'; i++) {
    		f = "";
    		// cout<<i<<" "<<mp[i]<<"
    ";
    		if(mp[i]&1) {
    			for(int j=0;j<mp[i];j++) {
    				f.push_back(i);
    			}
    			ret.push_back(f);
    		}
    	}
    	return ret;
     }
    
    }
    

      

  • 相关阅读:
    base.View.OpenParameter.CreateFrom打开历史单据的值是default
    创建日期时间大于启动日期时间
    下拉列表不显示空白选项的原因
    复制、下推、选单时计算类的实体服务规则不会执行
    选单返回数据以后会执行的事件方法
    判断单据体是否录入行
    供应商协同平台
    .net core获取运行时文件绝对路径
    gmock函数参数输出 备忘录
    リバース 終章
  • 原文地址:https://www.cnblogs.com/passion-sky/p/8568729.html
Copyright © 2020-2023  润新知