Showing posts with label Problem A. Show all posts
Showing posts with label Problem A. Show all posts

Sunday, June 22, 2014

Codeforces Round #253 (Div. 2) Problem A

(Not very difficult -.-)
A. Anton and Letters
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, Anton has found a set. The set consists of small English letters. Anton carefully wrote out all the letters from the set in one line, separated by a comma. He also added an opening curved bracket at the beginning of the line and a closing curved bracket at the end of the line.
Unfortunately, from time to time Anton would forget writing some letter and write it again. He asks you to count the total number of distinct letters in his set.
Input
The first and the single line contains the set of letters. The length of the line doesn't exceed 1000. It is guaranteed that the line starts from an opening curved bracket and ends with a closing curved bracket. Between them, small English letters are listed, separated by a comma. Each comma is followed by a space.
Output
Print a single number — the number of distinct letters in Anton's set.
Sample test(s)
input
{a, b, c}
output
3
input
{b, a, b, a}
output
2
input
{}
output
0


My Solution: [Java]

→ Source
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;


public class AntonNLetters {
 public static void main(String[] args){
  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  String input="";
  try{input = br.readLine();}catch(IOException e){System.out.print(e);}
  
  if(input.charAt(1)=='}'){
   System.out.println(0);
   return;
  }
  
  HashSet<Character> set = new HashSet<Character>();
  for(int i=1;i<=input.length()-1;i=i+3){
   set.add(input.charAt(i));
  }
  System.out.println(set.size());
 }
}

Saturday, May 3, 2014

StartUp [Coder-Strike 2014 - Finals (online edition, Div. 1)] Problem A

A. Start Up
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Recently, a start up by two students of a state university of city F gained incredible popularity. Now it's time to start a new company. But what do we call it?
The market analysts came up with a very smart plan: the name of the company should be identical to its reflection in a mirror! In other words, if we write out the name of the company on a piece of paper in a line (horizontally, from left to right) with large English letters, then put this piece of paper in front of the mirror, then the reflection of the name in the mirror should perfectly match the line written on the piece of paper.
There are many suggestions for the company name, so coming up to the mirror with a piece of paper for each name wouldn't be sensible. The founders of the company decided to automatize this process. They asked you to write a program that can, given a word, determine whether the word is a 'mirror' word or not.
Input
The first line contains a non-empty name that needs to be checked. The name contains at most 105 large English letters. The name will be written with the next sans serif font:


Output
Print 'YES' (without the quotes), if the given name matches its mirror reflection. Otherwise, print 'NO' (without the quotes).

Sample test(s)
input
AHA
output
YES
input
Z
output
NO
input
XO
output
NO
My Solution [ Java ] :
→ Source
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;

public class StartUpMirror {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        String st = s.next();
        StringBuffer rev = new StringBuffer();
        Set<Character> set = new HashSet<Character>(Arrays.asList('A','H','I','M','O','T','U','V','W','X','Y'));
        
        for(int i=0;i<st.length();++i){
            if(!set.contains(st.charAt(i))){System.out.println("NO");System.exit(0);}
            rev.append(st.charAt(i));
        }
        if(st.matches(rev.reverse().toString()))
        System.out.println("YES");
        else
        System.out.println("NO");
    }
}

Squats [Codeforces Round #242 (Div. 2)] Problem A

A. Squats
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.
For another exercise, Pasha needs exactly  hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?
Input
The first line contains integer n (2 ≤ n ≤ 200n is even). The next line contains n characters without spaces. These characters describe the hamsters' position: the i-th character equals 'X', if the i-th hamster in the row is standing, and 'x', if he is sitting.
Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters' position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.

Sample test(s)
input
4
xxXx
output
1
XxXx
input
2
XX
output
1
xX
input
6
xXXxXx
output
0
xXXxXx
→ Source
import java.util.Scanner;
public class Squats {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();

        String k = s.next();
        char h[] = new char[n];
        int up = 0, mn = 0;
        for (int i = 0; i < n; ++i) {
            h[i] = k.charAt(i);
            if (h[i] == 'X') {
                up++;
            }
        }

        if (up < n / 2) {
            for (int i = 0; i < n && up < n / 2; ++i) {
                if (h[i] == 'x') {
                    mn++;
                    h[i] = 'X';
                    up++;
                }
            }
        } else if (up > n / 2) {
            for (int i = 0; i < n && up > n / 2; ++i) {
                if (h[i] == 'X') {
                    mn++;
                    h[i] = 'x';
                    up--;
                }
            }
        } 
        System.out.println(mn);
        for(int i=0;i<n;++i){System.out.print(h[i]);}

    }
}

Police Recruits [Codeforces Round #244 (Div. 2)] Problem A

A. Police Recruits
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
The police department of your city has just started its journey. Initially, they don’t have any manpower. So, they started hiring new recruits in groups.
Meanwhile, crimes keeps occurring within the city. One member of the police force can investigate only one crime during his/her lifetime.
If there is no police officer free (isn't busy with crime) during the occurrence of a crime, it will go untreated.
Given the chronological order of crime occurrences and recruit hirings, find the number of crimes which will go untreated.
Input
The first line of input will contain an integer n (1 ≤ n ≤ 105), the number of events. The next line will contain n space-separated integers.
If the integer is -1 then it means a crime has occurred. Otherwise, the integer will be positive, the number of officers recruited together at that time. No more than 10 officers will be recruited at a time.
Output
Print a single integer, the number of crimes which will go untreated.
Sample test(s)
input
3
-1 -1 1
output
2
input
8
1 -1 1 -1 -1 1 1 1
output
1
input
11
-1 -1 2 -1 -1 -1 -1 -1 -1 -1 -1
output
8
Note
Lets consider the second example:
  1. Firstly one person is hired.
  2. Then crime appears, the last hired person will investigate this crime.
  3. One more person is hired.
  4. One more crime appears, the last hired person will investigate this crime.
  5. Crime appears. There is no free policeman at the time, so this crime will go untreated.
  6. One more person is hired.
  7. One more person is hired.
  8. One more person is hired.
The answer is one, as one crime (on step 5) will go untreated.

My Solution [Java] :
→ Source
import java.util.Scanner;

public class PoliceRecruits {
    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        int n = s.nextInt();
        int mc=0;   //police men counter
        int in=0;
        int c=0; //crimes
        for(int i=0;i<n;++i){
            in = s.nextInt();
            if(in>0){
                mc+=in;
            }
            if(in<0){
                if(mc<=0){
                    c++;
                }else
                mc--;
            }
        }
        System.out.println(c);
    }
}

Friday, May 2, 2014

Password Check [Codeforces Coder-Strike 2014 - Qualification Round] Problem A

A. Password Check
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You have probably registered on Internet sites many times. And each time you should enter your invented password. Usually the registration form automatically checks the password's crypt resistance. If the user's password isn't complex enough, a message is displayed. Today your task is to implement such an automatic check.
Web-developers of the company Q assume that a password is complex enough, if it meets all of the following conditions:
  • the password length is at least 5 characters;
  • the password contains at least one large English letter;
  • the password contains at least one small English letter;
  • the password contains at least one digit.
You are given a password. Please implement the automatic check of its complexity for company Q.
Input
The first line contains a non-empty sequence of characters (at most 100 characters). Each character is either a large English letter, or a small English letter, or a digit, or one of characters: "!", "?", ".", ",", "_".
Output
If the password is complex enough, print message "Correct" (without the quotes), otherwise print message "Too weak" (without the quotes).
Sample test(s)
input
abacaba
output
Too weak
input
X12345
output
Too weak
input
CONTEST_is_STARTED!!11
output
Correct
→ Source
import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordCheck {
    public static void main(String[] args) throws Exception{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s = br.readLine();
        
        if(s.length()<5){
            System.out.println("Too weak");
            System.exit(0);
        }
        int l=0,ss=0,d=0;
        
        Pattern p = Pattern.compile("[0-9]+");
        Matcher m = p.matcher(s);
        if(m.find()){
            d++;
        }
        p=Pattern.compile("[A-Z]+");
        m=p.matcher(s);
        if(m.find()){
            l++;
        }
        p=Pattern.compile("[a-z]+");
        m=p.matcher(s);
        if(m.find()){
            ss++;
        }
        
        if(l==0||ss==0||d==0){
            System.out.println("Too weak");
            System.exit(0);
        }
        System.out.println("Correct");
    }
}