2016年10月2日 星期日

Beautiful Binary String

Alice has a binary string, of length . She thinks a binary string is beautiful if and only if it doesn't contain thesubstring .
In one step, Alice can change a  to a  (or vice-versa). Count and print the minimum number of steps needed to make Alice see the string as beautiful.
Input Format
The first line contains an integer,  (the length of binary string ).
The second line contains a single binary string, , of length .
Constraints
  • Each character in .
Output Format
Print the minimum number of steps needed to make the string beautiful.
Sample Input 0
7
0101010
Sample Output 0
2
Sample Input 1
5
01100
Sample Output 1
0
Sample Input 2
10
0100101010
Sample Output 2
3
Explanation
Sample Case 0:
In this sample, 
The figure below shows a way to get rid of each instance of :
Because we were able to make the string beautiful by changing  characters ( and ), we print .
Sample Case 1:
In this sample 
The substring  does not occur in , so the string is already beautiful and we print .

====================================================
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String B = in.next();
        int i = 0, out = 0, next = 0;
        if (B.indexOf("010") >= 0) {
            while (i < B.length()) {
                next = B.indexOf("010", i) + 3;
                if (next < i) {
                    break;
                }
                i = next;    
                out++;
            }
        } else {
        }
        
        System.out.println(out);
    }
}

Strings: Making Anagrams

Alice is taking a cryptography class and finding anagrams to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example, bacdc and dcbac are anagrams, butbacdc and dcbad are not.
Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number?
Given two strings,  and , that may or may not be of the same length, determine the minimum number of character deletions required to make  and  anagrams. Any characters can be deleted from either of the strings.
This challenge is also available in the following translations:
Input Format
The first line contains a single string, .
The second line contains a single string, .
Constraints
  • It is guaranteed that  and  consist of lowercase English alphabetic letters (i.e.,  through ).
Output Format
Print a single integer denoting the number of characters you must delete to make the two strings anagrams of each other.
Sample Input
cde
abc
Sample Output
4
Explanation
We delete the following characters from our two strings to turn them into anagrams of each other:
  1. Remove d and e from cde to get c.
  2. Remove a and b from abc to get c.
We must delete  characters to make both strings anagrams, so we print  on a new line.

===================================================
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.Arrays;
public class Solution {
    public static int numberNeeded(String first, String second) {
       char[] chars = first.toCharArray();
       Arrays.sort(chars);
       String sortedFirst = new String(chars);
       
       chars = second.toCharArray();
       Arrays.sort(chars);
       String sortedSecond = new String(chars);
       
       int i=0, count=0;
       while (i < sortedFirst.length()) {
           char ch = sortedFirst.charAt(i);           
           if (sortedSecond.indexOf(ch) >= 0) {
               int val1, val2;
               val1 = sortedFirst.lastIndexOf(ch) - i + 1;
               val2 = sortedSecond.lastIndexOf(ch) - sortedSecond.indexOf(ch) + 1;
               i = sortedFirst.lastIndexOf(ch) + 1;               
               count += val1 < val2 ? val1 : val2;
           } else {
               i++;   
           }           
       }
        
        
       return first.length()-count + second.length()-count;
    }
  
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String a = in.next();
        String b = in.next();
        System.out.println(numberNeeded(a, b));
    }
}