Advertisement
coder0687

GoodNumbers

Jul 4th, 2021
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.77 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import static java.lang.System.*;
  4. import static java.lang.Math.*;
  5. import java.math.BigInteger;
  6. /*
  7. Question : Find Smallest Possible Good Numbers.A number is termed as a “GOOD NUMBER” if meets the 2 criteria’s mentioned below;
  8. a. All the digits in the number must be only 1 or 2 or 3
  9. b. No two adjacent substrings of the number is same
  10. Example for good numbers: 1, 121, 1213, 1213121
  11. Example for bad numbers: 11, 1212, 1221, 1231231
  12. 11 is a bad number because 1 follows 1.Similarly, 1212 is bad because the substring “12” is present adjacent to the substring “12” 1221 is a bad number because the substring “2” follows “2”
  13. 1231231 is a bad number because the substring “123” is adjacent to substring “123”
  14. Design an algorithm to accept an input value N (which is a positive non-zero number). The algorithm must then find out the smallest good number which can be formed and has only N digits in it. The number must be displayed as output.
  15. For Example – If the value on N entered by the user is 4
  16. The smallest possible good number which can be formed having 4 digits is 1213
  17. */
  18. class GoodNumbers
  19. {
  20.     String res = "";
  21.     void run() {
  22.         dfs("",new int[]{1,2,3},ni());
  23.         out.println(res);
  24.         out.close();
  25.     }
  26.     boolean dfs(String s,int a[],int n) {
  27.         if(s.length()==n) return true;
  28.         boolean b=false;
  29.         for(int i:a) if(isValid(s,i) && dfs(s+i,a,n)) { res=i+res; b=true; break; }
  30.         return b;
  31.     }
  32.     boolean isValid(String s, int i) {
  33.         if(s.length()==0) return true;
  34.         int l=1,n=s.length();
  35.         boolean b=true;
  36.         while(l<=(n+1)>>1 && b) {
  37.             if(s.substring(n-l+1-l,n-l+1).compareTo(s.substring(n-l+1)+i)==0) b=false;
  38.             l++;
  39.         }
  40.         return b;
  41.     }
  42.     public static void main(String[] args)throws Exception {
  43.         try {
  44.             // new Thread(null, new GoodNumbers()::run, "1", 1 << 26).start();
  45.             new Thread(null, new GoodNumbers("ONLINE_JUDGE")::run, "1", 1 << 26).start();
  46.         } catch(Exception e) {}
  47.     }
  48.     FastReader sc=null;PrintWriter out = null;
  49.     public GoodNumbers()throws Exception {
  50.         sc = new FastReader(new FileInputStream("D:\\CP\\input.txt"));
  51.         out = new PrintWriter(new BufferedWriter(new FileWriter("D:\\CP\\output.txt")));
  52.     }
  53.     public GoodNumbers(String JUDGE) {
  54.         sc = new FastReader(System.in);
  55.         out = new PrintWriter(System.out);      
  56.     }
  57.    
  58.     String ns() { return sc.next(); }
  59.     int ni() { return sc.nextInt(); }
  60.     long nl() { return sc.nextLong(); }
  61.     int[] ni(int n) {
  62.         int a[]=new int[n];
  63.         for(int i=0;i<n;a[i++]=ni());
  64.         return a;
  65.     }
  66.     long[] nl(int n) {
  67.         long a[]=new long[n];
  68.         for(int i=0;i<n;a[i++]=nl());
  69.         return a;
  70.     }
  71.    
  72.     int[][] ni(int n,int m) {
  73.         int a[][]=new int[n][m];
  74.         for(int i=0;i<n;i++)
  75.             for(int j=0;j<m;j++)
  76.                 a[i][j]=ni();
  77.         return a;
  78.     }
  79.     long[][] nl(int n,int m) {
  80.         long a[][]=new long[n][m];
  81.         for(int i=0;i<n;i++)
  82.             for(int j=0;j<m;j++)
  83.                 a[i][j]=nl();
  84.         return a;
  85.     }
  86.     int gcd(int a, int b) {
  87.         return b==0?a:gcd(b,a%b);
  88.     }
  89.     static class FastReader {
  90.         private InputStream stream;
  91.         private byte[] buf = new byte[1024];
  92.         private int curChar;
  93.         private int numChars;
  94.         private FastReader.SpaceCharFilter filter;
  95.         public FastReader(InputStream stream) {
  96.             this.stream = stream;
  97.         }
  98.         public int read() {
  99.             if (numChars == -1) throw new InputMismatchException();
  100.             if (curChar >= numChars) {
  101.                 curChar = 0;
  102.                 try {
  103.                     numChars = stream.read(buf);
  104.                 } catch (IOException e) {
  105.                     throw new InputMismatchException();
  106.                 }
  107.                 if (numChars <= 0) return -1;
  108.             }
  109.             return buf[curChar++];
  110.         }
  111.         public int nextInt() {
  112.             int c = read();
  113.             while (isSpaceChar(c)) c = read();
  114.             int sgn = 1;
  115.             if (c == '-') {
  116.                 sgn = -1;
  117.                 c = read();
  118.             }
  119.             int res = 0;
  120.             do {
  121.                 if (c < '0' || c > '9') throw new InputMismatchException();
  122.                 res *= 10;
  123.                 res += c - '0';
  124.                 c = read();
  125.             }
  126.             while (!isSpaceChar(c));
  127.             return res * sgn;
  128.         }
  129.        
  130.         public long nextLong() {
  131.             int c = read();
  132.             while (isSpaceChar(c)) c = read();
  133.             int sgn = 1;
  134.             if (c == '-') {
  135.                 sgn = -1;
  136.                 c = read();
  137.             }
  138.             long res = 0;
  139.             do {
  140.                 if (c < '0' || c > '9') throw new InputMismatchException();
  141.                 res = res*1L*10;
  142.                 res += c - '0';
  143.                 c = read();
  144.             }
  145.             while (!isSpaceChar(c));
  146.             return res *1L* sgn;
  147.         }
  148.        
  149.         public String next() {
  150.             int c = read();
  151.             while (isSpaceChar(c)) c = read();
  152.             StringBuilder res = new StringBuilder();
  153.             do {
  154.                 res.appendCodePoint(c);
  155.                 c = read();
  156.             } while (!isSpaceChar(c));
  157.             return res.toString();
  158.         }
  159.         public boolean isSpaceChar(int c) {
  160.             if (filter != null) return filter.isSpaceChar(c);
  161.             return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
  162.         }
  163.         public interface SpaceCharFilter {
  164.             public boolean isSpaceChar(int ch);
  165.         }
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement