problem statement
After reading the problem statement carefully and checking the sample test cases, I noticed this part of the problem "needs the lexicographically minimum one", and thought by myself if it is gonna be a substring of input and lucky and lexicographically minimum one, then it has to be "4" or "7" because other substrings that are lucky contain 4 and 7, and there is no lucky number other than "4" or "7" that is repeated more times than "4" or "7" because at least the lucky number is going to be "44", "77", "47", "74", and at this cases "4" and "7" are repeated more times than these substrings.
So I just iterated through input string and counted number of 4's and number of 7's, if there is no 4 or 7, then output -1, otherwise if cnt7 is less than cnt4 output 4 else output 7.
I describe my solution or give some hints about the solution for algorithmic problems used in ICPC or online sites for programming contests.
Showing posts with label Beginner. Show all posts
Showing posts with label Beginner. Show all posts
Friday, January 6, 2012
Codeforces Beta Round #91 (Div. 2 Only) - A. Lucky Division
problem statement
As you read in the problem statement, you have to find out whether a number has a divisor which is also a lucky number, so iterate from 1 to n, if n is divisible by i, then check whether it is a lucky number or not?
Easily write a function that take a number and tell whether it is lucky or not, in other words the function checks whether all of the digits in the given number are 4 or 7.
As you read in the problem statement, you have to find out whether a number has a divisor which is also a lucky number, so iterate from 1 to n, if n is divisible by i, then check whether it is a lucky number or not?
Easily write a function that take a number and tell whether it is lucky or not, in other words the function checks whether all of the digits in the given number are 4 or 7.
- bool isLucky(int n)
- {
- int rem;
- while(n)
- {
- rem = n%10;
- if(rem != 7 && rem != 4)
- return false;
- n /= 10;
- }
- return true;
- }
Thursday, December 8, 2011
TopCoder - SRM 515 DIV 2 - FortunateNumbers - getFortunate
As you can see in the problem statement just a simple brute force is enough to pass the time limit. I produce all combinations of a[i] + b[j] + c[k] for all i, j, k and check whether it is a fortunate number or not, if yes I insert the number in a set and finally I return the size of the set.
Tuesday, January 4, 2011
TopCoder - SRM 145 DIV 2 - DitherCounter - count
problem statement
In this problem you have to count how many of the characters of screen exist in dithered, to do this you can loop over the elements of screen and then for each element of screen search in dithered whether that character is there or not, O( n^3 ). But I do this O( n^2 ), since characters are only uppercase letters, at first I mark which characters are there in dithered, and then loop over screen and for each character it takes O( 1 ) to check whether it is in dithered or not.
In this problem you have to count how many of the characters of screen exist in dithered, to do this you can loop over the elements of screen and then for each element of screen search in dithered whether that character is there or not, O( n^3 ). But I do this O( n^2 ), since characters are only uppercase letters, at first I mark which characters are there in dithered, and then loop over screen and for each character it takes O( 1 ) to check whether it is in dithered or not.
TopCoder - SRM 144 DIV 2 - Time - whatTime
problem statement
In this problem, you have to convert a given second into it's equivalent standard time "h:m:s".
Easily I find how many 3600 are there in it, how many 60 are there left in it, and now I have h, m and s that I have to return a string not integers, so I convert each of them into a string and add them together in one string and return it.
Function f(int) just converts the given integer into a string like this:
int f( int n )
{
if( n == 0 )
return "0";
string res = "";
while( n )
{
res += n%10 + '0';
n /= 10;
}
reverse( res.begin(), res.end() );
return res;
}
In this problem, you have to convert a given second into it's equivalent standard time "h:m:s".
Easily I find how many 3600 are there in it, how many 60 are there left in it, and now I have h, m and s that I have to return a string not integers, so I convert each of them into a string and add them together in one string and return it.
int h = seconds/3600; int m = (seconds%3600)/60; int s = seconds%60;return f(h) + ":" + f(m) + ":" + f(s);
Function f(int) just converts the given integer into a string like this:
int f( int n )
{
if( n == 0 )
return "0";
string res = "";
while( n )
{
res += n%10 + '0';
n /= 10;
}
reverse( res.begin(), res.end() );
return res;
}
Subscribe to:
Posts (Atom)
USACO - Prime Palindromes
I just skimmed the problem statement and panicked of the high boundary of the input, but something inside told me don't worry everyth...
-
Prime Cryptarithm The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set ...
-
Palindromic Squares Rob Kolstad Palindromes are numbers that read the same forwards as backwards. The number 12321 is a typical palindr...
-
I don't know why, but I had a great misunderstanding of the problem statement from this sentence "FJ pours milk from one bucket t...