Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- class SuperString{
- List<String> list;
- Stack<String> stack;
- boolean reversed;
- public SuperString(){
- list = new LinkedList<String>();
- stack = new Stack<>();
- reversed = false;
- }
- public void addToStack(String s){
- if(reversed){
- StringBuilder sb = new StringBuilder(s);
- stack.push(sb.reverse().toString());
- }else{
- stack.push(s);
- }
- }
- public void append(String s){
- list.add(s);
- addToStack(s);
- }
- public void insert(String s){
- list.add(0, s);
- addToStack(s);
- }
- public void reverse(){
- List<String> newList = new LinkedList<String>();
- Stack<String> newStack = new Stack<String>();
- for(String curr : list){
- StringBuilder sb = new StringBuilder(curr);
- sb.reverse();
- curr = sb.toString();
- newList.add(0, curr);
- }
- reversed = !reversed;
- list = newList;
- }
- public String toString(){
- StringBuilder sb = new StringBuilder();
- for(String curr: list){
- sb.append(curr);
- }
- return sb.toString();
- }
- public void removeLast(int k){
- for(int i = 0 ; i < k; i++){
- String temp = stack.pop();
- if(reversed){
- StringBuilder sb = new StringBuilder(temp);
- list.remove(sb.reverse().toString());
- }else{
- list.remove(temp);
- }
- }
- }
- public boolean contains(String s){
- return toString().contains(s);
- }
- }
- public class SuperStringTest {
- public static void main(String[] args) {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if ( k == 0 ) {
- SuperString s = new SuperString();
- while ( true ) {
- int command = jin.nextInt();
- if ( command == 0 ) {//append(String s)
- s.append(jin.next());
- }
- if ( command == 1 ) {//insert(String s)
- s.insert(jin.next());
- }
- if ( command == 2 ) {//contains(String s)
- System.out.println(s.contains(jin.next()));
- }
- if ( command == 3 ) {//reverse()
- s.reverse();
- }
- if ( command == 4 ) {//toString()
- System.out.println(s);
- }
- if ( command == 5 ) {//removeLast(int k)
- s.removeLast(jin.nextInt());
- }
- if ( command == 6 ) {//end
- break;
- }
- }
- }
- }
- }
- import java.util.*;
- class IntegerList{
- List<Integer> list;
- public IntegerList(){
- list = new ArrayList<Integer>();
- }
- public IntegerList(Integer [] numbers){
- list = new ArrayList<Integer>();
- Collections.addAll(list, numbers);
- }
- public void add(int el, int idx){
- if( idx < list.size() ) {
- list.add(idx, el);
- }else if( idx == list.size() ) {
- list.add(el);
- }else{
- int k = idx - list.size();
- for(int i = 0; i < k; i++){
- list.add(0);
- }
- list.add(el);
- }
- }
- public Integer remove(int idx) {
- if (idx < 0&&idx >= list.size()) throw new ArrayIndexOutOfBoundsException();
- Integer ret = list.get(idx);
- list.remove(idx);
- return ret;
- }
- public int count(int el) {
- int counter = 0;
- for(Integer curr : list){
- if( curr.equals(el) )
- counter++;
- }
- return counter;
- }
- public void removeDuplicates() {
- for(int i = list.size()-1 ; i > 0; i--){
- list.subList(0, i).remove( list.get(i) );
- }
- }
- public IntegerList addValue(int value) {
- Integer [] arr= new Integer [list.size()];
- for(int i = 0; i < list.size(); i++){
- arr[i] = list.get(i) + value;
- }
- return new IntegerList(arr);
- }
- public void shiftLeft(int idx, int k) {
- while( k >= list.size() ) k-=list.size();
- int pom = idx - k;
- if( pom < 0 ) pom+=list.size();
- Integer el = list.get(idx);
- list.remove(idx);
- list.add(pom, el);
- }
- public void shiftRight(int idx, int k) {
- while( k >= list.size() ) k-=list.size();
- int pom = idx + k;
- if( pom >= list.size() ) pom-=list.size();
- Integer el = list.get(idx);
- list.remove(idx);
- list.add(pom, el);
- }
- public int sumFirst(int idx) {
- if( idx > list.size() ) idx = list.size();
- int sum = 0;
- for(int i = 0; i < idx; i++){
- sum += list.get(i);
- }
- return sum;
- }
- public int sumLast(int idx) {
- if( idx > list.size() ) idx = list.size();
- int sum = 0;
- for(int i = 0; i < idx; i++){
- sum += list.get( list.size() - 1 - i );
- }
- return sum;
- }
- public int size() {
- return list.size();
- }
- public int get(int idx) {
- return list.get(idx);
- }
- public String toString(){
- StringBuilder sb = new StringBuilder();
- for(Integer curr : list){
- sb.append(curr.toString()+" ");
- }
- return sb.toString();
- }
- }
- public class IntegerListTest {
- public static void main(String[] args) throws Exception {
- Scanner jin = new Scanner(System.in);
- int k = jin.nextInt();
- if ( k == 0 ) { //test standard methods
- int subtest = jin.nextInt();
- if ( subtest == 0 ) {
- IntegerList list = new IntegerList();
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) {
- list.add(jin.nextInt(), jin.nextInt());
- }
- if ( num == 1 ) {
- list.remove(jin.nextInt());
- }
- if ( num == 2 ) {
- print(list);
- }
- if ( num == 3 ) {
- break;
- }
- }
- }
- if ( subtest == 1 ) {
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- print(list);
- }
- }
- if ( k == 1 ) { //test count,remove duplicates, addValue
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) { //count
- System.out.println(list.count(jin.nextInt()));
- }
- if ( num == 1 ) {
- list.removeDuplicates();
- }
- if ( num == 2 ) {
- print(list.addValue(jin.nextInt()));
- }
- if ( num == 3 ) {
- list.add(jin.nextInt(), jin.nextInt());
- }
- if ( num == 4 ) {
- print(list);
- }
- if ( num == 5 ) {
- break;
- }
- }
- }
- if ( k == 2 ) { //test shiftRight, shiftLeft, sumFirst , sumLast
- int n = jin.nextInt();
- Integer a[] = new Integer[n];
- for ( int i = 0 ; i < n ; ++i ) {
- a[i] = jin.nextInt();
- }
- IntegerList list = new IntegerList(a);
- while ( true ) {
- int num = jin.nextInt();
- if ( num == 0 ) { //count
- list.shiftLeft(jin.nextInt(), jin.nextInt());
- }
- if ( num == 1 ) {
- list.shiftRight(jin.nextInt(), jin.nextInt());
- }
- if ( num == 2 ) {
- System.out.println(list.sumFirst(jin.nextInt()));
- }
- if ( num == 3 ) {
- System.out.println(list.sumLast(jin.nextInt()));
- }
- if ( num == 4 ) {
- print(list);
- }
- if ( num == 5 ) {
- break;
- }
- }
- }
- }
- public static void print(IntegerList il) {
- if ( il.size() == 0 ) System.out.print("EMPTY");
- for ( int i = 0 ; i < il.size() ; ++i ) {
- if ( i > 0 ) System.out.print(" ");
- System.out.print(il.get(i));
- }
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement