Wednesday, September 16, 2015

Analogous Issues (CPROM UiTM Arau 2015)

You are given an analogy of the form A::B, where B is an anagram of the word A. You must compute the same analogy for a similar-sized word M by rearranging the letters of M to create a new word N in the same way as described by the analogy.

For example, given the analogy RAIN::IRAN above, the similar analogy for the word ACRE is RACE. Note how the shuffling of letters from A to B is replicated to create N from M. The word A contains no duplicate letters. The same is true for B and M. All strings (A, B, M and N) will have between 2 and 10 characters each.

Input
The first line of the input contains the analogy in the form of two strings A and B of the same length separated by two colons (“::”). The second line will contain a string M of the same length as A and B.  
Output
The output of the program should print the result of single string representing the analogy for the input word M. The length of this output string must be the same as the length of M.
The output must be written to standard output.




 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Solved By Afiq
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;

public class Q7{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      String line =System.getProperty("line.separator");
      scan.useDelimiter(line);      
      
      String[] samples = scan.next().split("::");
      String str = scan.next();
      
      int[] arrange = new int[str.length()];
      
      String fstpermu = samples[0];
      String secpermu = samples[1];
      for(int x=0;x<fstpermu.length();x++){
         arrange[x] = secpermu.indexOf(fstpermu.charAt(x));
      }            
      
      String rearrange = "";
      for(int x=0;x<arrange.length;x++){
         for(int y=0;y<arrange.length;y++){
            if(x == arrange[y])
               rearrange = rearrange + str.charAt(y);
         }
      }
      
      System.out.println(rearrange);

   }
}

No comments:

Post a Comment