码迷,mamicode.com
首页 > 其他好文 > 详细

Topcoder712B

时间:2018-03-14 18:10:37      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:parameter   solution   out   bre   contains   eve   rap   ==   like   

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]<<"\n";
		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<<"\n";
			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]<<"\n";
		if(mp[i]&1) {
			for(int j=0;j<mp[i];j++) {
				f.push_back(i);
			}
			ret.push_back(f);
		}
	}
	return ret;
 }

}

  

Topcoder712B

标签:parameter   solution   out   bre   contains   eve   rap   ==   like   

原文地址:https://www.cnblogs.com/passion-sky/p/8568729.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!