夢追い人

"It takes a dreamer to make a dream come true."―Vincent Willem van Gogh

SRM149 Div.2 Easy FormatAmt

風邪だといいんですが…

父の話だとどうも花粉症っぽい。

char->int?int->string?変換が重要。

このブログでも幾度となくstringstreamを扱ってはきましたが、今回はそれの応用。

めっちゃ苦労しました。
問題文がこちら。

PROBLEM STATEMENT
In documents, it is frequently necessary to write monetary 
amounts in a 
standard format.  We have decided to format amounts as 
follows:
    the amount must start with '$'
    the amount should have a leading '0' if and only if it 
is less then 1 dollar.
    the amount must end with a decimal point and exactly 2 
following digits.
    the digits to the left of the decimal point must be 
separated into groups of three by commas (a group of one 
or two digits may appear on the left).


Create a class FormatAmt that contains a method amount 
that takes two int's, dollars and cents, as inputs and 
returns the properly formatted string.



DEFINITION
Class:FormatAmt
Method:amount
Parameters:int, int
Returns:string
Method signature:string amount(int dollars, int cents)


NOTES
  • One dollar is equal to 100 cents.
CONSTRAINTS
  • dollars will be between 0 and 2,000,000,000 inclusive
  • cents will be between 0 and 99 inclusive

でこんなコードになった。

class FormatAmt {
   public:
   string amount(int dollars, int cents)
  {
  	stringstream ss;
  	char doll[10];
  	sprintf(doll, "%d", dollars);
  	string s = string(doll);
	reverse(s.begin(), s.end());
	string ans = "";
	for (int i=0; i<s.length(); i++) {
		ans += s[i];
		if(i % 3 == 2 && i+1 < s.length()) {
			ans += ',';
		}
	}
	ans += '$';
	reverse(ans.begin(), ans.end());
	ss << cents;
	string cent = ss.str();
	if (cent.length() == 1) {
		char tmp = cent[0];
		cent[0] = '0';
		cent.push_back(tmp);
	}
	ans += '.'; ans += cent;
	return ans;
  }
};

二回もリバースしてます。

stringの前に文字を挿入する方法あればこんな手間いらない?・・・いや、下から三つごとにカンマ入れなきゃいけないんでそうとも限りません。

というわけでした。

p.s.
ついにTCO始まりましたね。まぁ招待制みたいなんで僕はもちろん参加できずに同じ問題を解くMMに参加しただけですが・・・
一応問題文だけ載せておきましょうか。まだ僕も解読していませんが。。。

Problem Statement

 *TCO competitors (except those who received a bye to Round 2) must not submit solutions to MM 69. Doing so may result in disqualification.*

A black-and-white bitmap image of height H and width W is created by drawing nLetter black letters of various sizes on white foreground. You can't see the whole image, but you can figure out its contents by scanning its rows and analyzing the results. Your task is to restore the image as fully as possible.

Your code should implement a method restore. Its parameters give you H, W, nLetter and the number of black pixels in the image nb. You can call a static library function scan(R) in class ImageToScan which performs a scan of row R (0-based) of the image and returns its contents as a string of length W. Character i of return string represents the contents of pixel in row R, column i (0-based) of the original image: '0' for white, '1' for black. You must return your guess for the image as vector . Your return must contain H elements, each having W characters, and only '0' and '1' characters in it. The j-th character in i-th element must describe the color of pixel in row i, column j of the image.
Your score for an individual test case will be calculated as follows. All unscanned rows of your return will be compared with the correct image pixel by pixel. The number of correctly guessed pixels CP and the total number of white pixels in the unscanned rows of the original image WP will be calculated. The score is then defined as max{0, (CP - WP) / nb}. Your overall score will be the sum of individual scores over all test cases.

Invalid return of any kind results in zero score for this test case. Attempts to scan non-existing row and attempts to scan the same row more than once are considered to be invalid. The return value of scan function for such scans is an empty string. Making an invalid scan results in zero score for this test case, even if your code subsequently returns a valid image description.

The test cases are generated as follows: H, W and nLetter are chosen randomly and uniformly. After this, the letters are drawn on the image one by one. For each letter its character (A..Z), font size (8..28) and font style (plain or bold) are chosen randomly and uniformly. The corresponding image is fetched from the pre-generated list of letter images. Next, the location for this letter is chosen at random, so that the letter is completely located within the image and it doesn't intersect any of the previously placed letters too much. To be more specific, for each letter its smallest bounding rectangle is stored, and for each pair of placed letters the sum of dimensions of intersection of their bounding rectangles can be at most half of the sum of the dimensions of any of these two bounding rectangles. If 100 attempts to place the new letter fail, it is placed without checking for intersections. Finally, nb is calculated from the resulting image.

The letter images used for generation are available for download. Each image is stored in a separate file with name consisting of three parts: letter, font style ('P' for plain, 'B' for bold) and font size. Each file starts from two lines containing height H0 and width W0 of the letter's bounding rectangle. It is continued with letter image description: H0 rows, each containing W0 characters '0'/'1'.

A visualizer is available for offline testing. You can check its source code for a precise implementation of test case generation and scoring.

Definition

Class: ImageScanner
Method: restore
Parameters: int, int, int, int
Returns: vector 
Method signature: vector  restore(int H, int W, int nb, int nLetter)
(be sure your methods are public)

Available Libraries

Class: ImageToScan
Method: scan
Parameters: int
Returns: string
Sample Call: val = ImageToScan::scan(row);

Notes

  • The memory limit is 1024 MB and the time limit is 10 seconds (which includes only time spent in your code).
  • There is no explicit code size limit. The implicit source code size limit is around 1 MB (it is not advisable to submit codes of size close to that or larger).
  • There are 10 example test cases and 100 full submission test cases.
Constraints
  • H and W will each be between 50 and 300, inclusive (except for example 0).
  • nLetter will be between W*H/400 and W*H/200, inclusive.

では。