Advertisement
Guest User

Untitled

a guest
Dec 12th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1. package seraphic;
  2. import java.io.IOException;
  3. import java.nio.file.Files;
  4. import java.nio.file.Paths;
  5. import java.util.StringTokenizer;
  6. import java.util.stream.Collectors;
  7. import java.util.stream.Stream;
  8.  
  9.  
  10.  
  11. public class Seraphic {
  12.    
  13.     public static String readin()
  14.     {
  15.         String path = "src/seraphic/in.txt";
  16.         Stream<String> lines;
  17.         try {
  18.             lines = Files.lines(Paths.get(path));
  19.             String ret = lines.collect(Collectors.joining(System.lineSeparator()));
  20.             lines.close();
  21.             return(ret);
  22.         } catch (IOException e)
  23.         {
  24.             // TODO Auto-generated catch block
  25.             e.printStackTrace();
  26.         }
  27.         return("Error");
  28.     }
  29.     static class LinkListElem
  30.     {
  31.         int data;
  32.         LinkListElem Next;
  33.         LinkListElem(int datain)
  34.         {
  35.             Next = null;
  36.             data = datain;
  37.         }
  38.     }
  39.    
  40.     static class LinkList
  41.     {
  42.         LinkListElem head = null;
  43.         LinkListElem end = null;
  44.     }
  45.    
  46.     public static LinkList LinkListCreate()
  47.     {
  48.         LinkList l1 = new LinkList();
  49.         l1.head = null;
  50.         l1.end = null;
  51.         return l1;
  52.     }
  53.     public static LinkList add(int data, LinkList L)
  54.     {
  55.         LinkListElem newElem = new LinkListElem(data);
  56.         if(L.head==null)
  57.         {
  58.             L.head = newElem;
  59.             L.end = newElem;
  60.         }else
  61.         {
  62.             L.end.Next = newElem;
  63.             L.end = newElem;
  64.         }
  65.         return L;
  66.     }
  67.    
  68.     public static int ListLen(LinkList L)
  69.     {
  70.         LinkListElem curElem = L.head;
  71.         int sum = 0;
  72.         while(curElem != null)
  73.         {
  74.             sum += 1;
  75.             curElem = curElem.Next;
  76.         }
  77.         return sum;
  78.     }
  79.    
  80.    
  81.     public static int ListIdx(LinkList L, int idx)
  82.     {
  83.         LinkListElem curElem = L.head;
  84.         int i = 0;
  85.         while(i < idx)
  86.         {
  87.             i+=1;
  88.             curElem = curElem.Next;
  89.         }
  90.         return curElem.data;
  91.     }
  92.    
  93.     public static boolean partitioncheck(LinkList L, int s1, int s2, int l1, int l2, int idx)
  94.     {
  95.         if(idx >= ListLen(L))
  96.         {
  97.             if((java.lang.Math.abs(l1-l2)<=1)&&(s1-s2==0))
  98.             {
  99.                 return true;
  100.             }else
  101.             {
  102.                 return false;
  103.             }
  104.         }
  105.         if(partitioncheck(L, s1+ListIdx(L, idx), s2, l1+1, l2, idx+1)||partitioncheck(L, s1, s2+ListIdx(L, idx), l1, l2+1, idx+1))
  106.         {
  107.             return true;
  108.         }else
  109.         {
  110.             return false;
  111.         }
  112.     }
  113.     public static void pcheck(LinkList L)
  114.     {
  115.         if(partitioncheck(L, 0, 0, 0, 0, 0))
  116.         {
  117.             System.out.println("YES");
  118.         }else
  119.         {
  120.             System.out.println("NO");
  121.         }
  122.     }
  123.  
  124.  
  125.     public static void main(String[] args)
  126.     {
  127.         LinkList L1 = LinkListCreate();
  128.         String input = readin();
  129.         StringTokenizer commarem = new StringTokenizer(input, " ");
  130.         int c = 0;
  131.    
  132.         while (commarem.hasMoreElements())
  133.         {
  134.             c = Integer.parseInt((String) (commarem.nextElement()));
  135.             add(c ,L1);
  136.         }
  137.         pcheck(L1);
  138.        
  139.     }
  140.  
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement