Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package seraphic;
- import java.io.IOException;
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.StringTokenizer;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- public class Seraphic {
- public static String readin()
- {
- String path = "src/seraphic/in.txt";
- Stream<String> lines;
- try {
- lines = Files.lines(Paths.get(path));
- String ret = lines.collect(Collectors.joining(System.lineSeparator()));
- lines.close();
- return(ret);
- } catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return("Error");
- }
- static class LinkListElem
- {
- int data;
- LinkListElem Next;
- LinkListElem(int datain)
- {
- Next = null;
- data = datain;
- }
- }
- static class LinkList
- {
- LinkListElem head = null;
- LinkListElem end = null;
- }
- public static LinkList LinkListCreate()
- {
- LinkList l1 = new LinkList();
- l1.head = null;
- l1.end = null;
- return l1;
- }
- public static LinkList add(int data, LinkList L)
- {
- LinkListElem newElem = new LinkListElem(data);
- if(L.head==null)
- {
- L.head = newElem;
- L.end = newElem;
- }else
- {
- L.end.Next = newElem;
- L.end = newElem;
- }
- return L;
- }
- public static int ListLen(LinkList L)
- {
- LinkListElem curElem = L.head;
- int sum = 0;
- while(curElem != null)
- {
- sum += 1;
- curElem = curElem.Next;
- }
- return sum;
- }
- public static int ListIdx(LinkList L, int idx)
- {
- LinkListElem curElem = L.head;
- int i = 0;
- while(i < idx)
- {
- i+=1;
- curElem = curElem.Next;
- }
- return curElem.data;
- }
- public static boolean partitioncheck(LinkList L, int s1, int s2, int l1, int l2, int idx)
- {
- if(idx >= ListLen(L))
- {
- if((java.lang.Math.abs(l1-l2)<=1)&&(s1-s2==0))
- {
- return true;
- }else
- {
- return false;
- }
- }
- 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))
- {
- return true;
- }else
- {
- return false;
- }
- }
- public static void pcheck(LinkList L)
- {
- if(partitioncheck(L, 0, 0, 0, 0, 0))
- {
- System.out.println("YES");
- }else
- {
- System.out.println("NO");
- }
- }
- public static void main(String[] args)
- {
- LinkList L1 = LinkListCreate();
- String input = readin();
- StringTokenizer commarem = new StringTokenizer(input, " ");
- int c = 0;
- while (commarem.hasMoreElements())
- {
- c = Integer.parseInt((String) (commarem.nextElement()));
- add(c ,L1);
- }
- pcheck(L1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement