Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
- import java.util.StringTokenizer;
- class DLLNode<E> {
- protected E element;
- protected DLLNode<E> pred, succ;
- public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
- this.element = elem;
- this.pred = pred;
- this.succ = succ;
- }
- @Override
- public String toString() {
- return element.toString();
- }
- }
- class DLL<E extends Comparable<E>> {
- private DLLNode<E> first, last;
- public DLL() {
- // Construct an empty SLL
- this.first = null;
- this.last = null;
- }
- public void deleteList() {
- first = null;
- last = null;
- }
- public int length() {
- int ret;
- if (first != null) {
- DLLNode<E> tmp = first;
- ret = 1;
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret++;
- }
- return ret;
- } else
- return 0;
- }
- public DLLNode<E> find(E o) {
- if (first != null) {
- DLLNode<E> tmp = first;
- while (tmp.element != o && tmp.succ != null)
- tmp = tmp.succ;
- if (tmp.element == o) {
- return tmp;
- } else {
- System.out.println("Elementot ne postoi vo listata");
- }
- } else {
- System.out.println("Listata e prazna");
- }
- return first;
- }
- public void insertFirst(E o) {
- DLLNode<E> ins = new DLLNode<E>(o, null, first);
- if (first == null)
- last = ins;
- else
- first.pred = ins;
- first = ins;
- }
- public void insertLast(E o) {
- if (first == null)
- insertFirst(o);
- else {
- DLLNode<E> ins = new DLLNode<E>(o, last, null);
- last.succ = ins;
- last = ins;
- }
- }
- public void insertAfter(E o, DLLNode<E> after) {
- if (after == last) {
- insertLast(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
- after.succ.pred = ins;
- after.succ = ins;
- }
- public void insertBefore(E o, DLLNode<E> before) {
- if (before == first) {
- insertFirst(o);
- return;
- }
- DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
- before.pred.succ = ins;
- before.pred = ins;
- }
- public E deleteFirst() {
- if (first != null) {
- DLLNode<E> tmp = first;
- first = first.succ;
- if (first != null) first.pred = null;
- if (first == null)
- last = null;
- return tmp.element;
- } else
- return null;
- }
- public E deleteLast() {
- if (first != null) {
- if (first.succ == null)
- return deleteFirst();
- else {
- DLLNode<E> tmp = last;
- last = last.pred;
- last.succ = null;
- return tmp.element;
- }
- }
- // else throw Exception
- return null;
- }
- public E delete(DLLNode<E> node) {
- if (node == first) {
- deleteFirst();
- return node.element;
- }
- if (node == last) {
- deleteLast();
- return node.element;
- }
- node.pred.succ = node.succ;
- node.succ.pred = node.pred;
- return node.element;
- }
- @Override
- public String toString() {
- String ret = new String();
- if (first != null) {
- DLLNode<E> tmp = first;
- ret += tmp + "<->";
- while (tmp.succ != null) {
- tmp = tmp.succ;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public String toStringR() {
- String ret = new String();
- if (last != null) {
- DLLNode<E> tmp = last;
- ret += tmp + "<->";
- while (tmp.pred != null) {
- tmp = tmp.pred;
- ret += tmp + "<->";
- }
- } else
- ret = "Prazna lista!!!";
- return ret;
- }
- public DLLNode<E> getFirst() {
- return first;
- }
- public DLLNode<E> getLast() {
- return last;
- }
- public void deleteDuplicates() {
- /*if (first != null) {
- DLLNode<E> tmp = first, tmp2 = tmp.succ;
- while (tmp.succ != null) {
- while (tmp2 != null) {
- if (tmp.element.compareTo(tmp2.element) == 0) {
- tmp2 = tmp2.succ;
- this.delete(tmp2.pred);
- } else
- tmp2 = tmp2.succ;
- }
- tmp = tmp.succ;
- tmp2 = tmp.succ;
- }
- } else
- return;*/
- if (first != null) {
- DLLNode<E> current = first;
- DLLNode<E> following = first.succ;
- while (current.succ != null) {
- while (following != null) {
- if (current.element.equals(following.element)) {
- following = following.succ;
- delete(following.pred);
- } else {
- following = following.succ;
- }
- }
- current = current.succ;
- following = current.succ;
- }
- }
- return;
- }
- }
- public class WeightedSum {
- /*
- Sample input
- 10 3
- 1 9 2 3 6 1 3 2 1 3
- Sample output
- 37
- */
- /* 1⋅првиотброј+2⋅вториотброј+…+K⋅K−тиотброј */
- static int solve(int numbers[], int len, int choose) {
- int matrix[][] = new int[len][choose];
- int result = 0;
- for (int i = 1; i < len; i++) {
- for (int j = 1; j < choose; j++) {
- for (int k = 0; k < i; k++) {
- int abs = j * numbers[k];
- if (matrix[i][j] < matrix[k][j - 1] + abs) {
- matrix[i][j] = matrix[k][j - 1] + abs;
- }
- }
- if (matrix[i][j] > result) {
- result = matrix[i][j];
- }
- }
- }
- for (int i = 0; i < matrix.length; i++) {
- for (int j = 0; j < matrix[0].length; j++) {
- System.out.print(matrix[i][j] + " ");
- }
- System.out.println();
- }
- return result;
- }
- public static void swap(final int a[], final int i, final int j) {
- final int temp = a[i];
- a[i] = a[j];
- a[j] = temp;
- }
- public static void printArray(final int a[]) {
- for (int i = 0; i < a.length; i++) {
- System.out.print(a[i] + " ");
- }
- System.out.println();
- }
- public static void printArray(final String a[]) {
- for (int i = 0; i < a.length; i++) {
- System.out.print(a[i] + "");
- }
- System.out.println();
- }
- public static void main(String[] args) throws Exception {
- /*BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
- StringTokenizer st = new StringTokenizer(br.readLine());
- int N = Integer.parseInt(st.nextToken());
- int K = Integer.parseInt(st.nextToken());
- int numbers[] = new int[N];
- st = new StringTokenizer(br.readLine());
- for (int i = 0; i < N; i++) {
- numbers[i] = Integer.parseInt(st.nextToken());
- }
- int res = solve(numbers, N, K);
- System.out.println(res);
- br.close();
- */
- /*int digits[] = new int[10];
- int size = 7;
- for (int i = 0; i < size; i++) {
- digits[i] = i;
- }
- printArray(digits);
- for (int i = size; i > 0; i--) {
- swap(digits, i, 2 + i);
- }
- printArray(digits);
- int index = 3;
- int elemsToMove = size - index + 1;
- System.arraycopy(digits, index, digits, index+1, elemsToMove);
- printArray(digits);*/
- /*
- // Deletes Duplicates
- DLL<Integer> list = new DLL<>();
- for (int i = 0; i < 5; i++) {
- list.insertLast(i);
- }
- for (int i = 1; i < 6; i++) {
- list.insertLast(i);
- }
- System.out.println(list);
- list.deleteDuplicates();
- System.out.println(list);*/
- DLL<Character> list = new DLL<>();
- String letters = "abcdefghij";
- char characters[] = letters.toCharArray();
- for (int i = 0; i < characters.length; i++) {
- list.insertLast((Character) characters[i]);
- }
- System.out.println(list);
- // From the beginning
- //DLLNode first = list.getFirst();
- //DLLNode last = list.getLast();
- /*for (DLLNode first = list.getFirst(); first != null; first = first.succ){
- list.delete(first.succ);
- }
- System.out.println(list);*/
- System.out.println(list.length());
- while (list.length() != 1) {
- DLLNode first = list.getFirst();
- //System.out.println("First Element: " + first.element);
- while (first != null) {
- if (first.succ != null) {
- list.delete(first.succ);
- }
- first = first.succ;
- }
- System.out.println("From Front");
- System.out.println(list);
- DLLNode last = list.getLast();
- //System.out.println("Last Element: " + last.element);
- while (last != null) {
- //System.out.println("While Last Element: " + last.element);
- if (last.pred != null) {
- list.delete(last.pred);
- }
- last = last.pred;
- }
- System.out.println("From Back");
- System.out.println(list);
- }
- System.out.println("\nFINAL\n");
- System.out.println(list);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment