Advertisement
Guest User

6ta

a guest
Jan 24th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.87 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. class SuperString{
  4. List<String> list;
  5. Stack<String> stack;
  6. boolean reversed;
  7.  
  8. public SuperString(){
  9. list = new LinkedList<String>();
  10. stack = new Stack<>();
  11. reversed = false;
  12. }
  13. public void addToStack(String s){
  14. if(reversed){
  15. StringBuilder sb = new StringBuilder(s);
  16. stack.push(sb.reverse().toString());
  17. }else{
  18. stack.push(s);
  19. }
  20. }
  21. public void append(String s){
  22. list.add(s);
  23. addToStack(s);
  24. }
  25. public void insert(String s){
  26. list.add(0, s);
  27. addToStack(s);
  28. }
  29. public void reverse(){
  30. List<String> newList = new LinkedList<String>();
  31. Stack<String> newStack = new Stack<String>();
  32. for(String curr : list){
  33. StringBuilder sb = new StringBuilder(curr);
  34. sb.reverse();
  35. curr = sb.toString();
  36. newList.add(0, curr);
  37. }
  38. reversed = !reversed;
  39. list = newList;
  40. }
  41. public String toString(){
  42. StringBuilder sb = new StringBuilder();
  43. for(String curr: list){
  44. sb.append(curr);
  45. }
  46. return sb.toString();
  47. }
  48. public void removeLast(int k){
  49. for(int i = 0 ; i < k; i++){
  50. String temp = stack.pop();
  51. if(reversed){
  52. StringBuilder sb = new StringBuilder(temp);
  53. list.remove(sb.reverse().toString());
  54. }else{
  55. list.remove(temp);
  56. }
  57. }
  58. }
  59. public boolean contains(String s){
  60. return toString().contains(s);
  61. }
  62. }
  63.  
  64.  
  65. public class SuperStringTest {
  66.  
  67. public static void main(String[] args) {
  68. Scanner jin = new Scanner(System.in);
  69. int k = jin.nextInt();
  70. if ( k == 0 ) {
  71. SuperString s = new SuperString();
  72. while ( true ) {
  73. int command = jin.nextInt();
  74. if ( command == 0 ) {//append(String s)
  75. s.append(jin.next());
  76. }
  77. if ( command == 1 ) {//insert(String s)
  78. s.insert(jin.next());
  79. }
  80. if ( command == 2 ) {//contains(String s)
  81. System.out.println(s.contains(jin.next()));
  82. }
  83. if ( command == 3 ) {//reverse()
  84. s.reverse();
  85. }
  86. if ( command == 4 ) {//toString()
  87. System.out.println(s);
  88. }
  89. if ( command == 5 ) {//removeLast(int k)
  90. s.removeLast(jin.nextInt());
  91. }
  92. if ( command == 6 ) {//end
  93. break;
  94. }
  95. }
  96. }
  97. }
  98.  
  99. }
  100.  
  101.  
  102. import java.util.*;
  103.  
  104. class IntegerList{
  105. List<Integer> list;
  106. public IntegerList(){
  107. list = new ArrayList<Integer>();
  108. }
  109. public IntegerList(Integer [] numbers){
  110. list = new ArrayList<Integer>();
  111. Collections.addAll(list, numbers);
  112. }
  113. public void add(int el, int idx){
  114. if( idx < list.size() ) {
  115. list.add(idx, el);
  116. }else if( idx == list.size() ) {
  117. list.add(el);
  118. }else{
  119. int k = idx - list.size();
  120. for(int i = 0; i < k; i++){
  121. list.add(0);
  122. }
  123. list.add(el);
  124. }
  125. }
  126. public Integer remove(int idx) {
  127. if (idx < 0&&idx >= list.size()) throw new ArrayIndexOutOfBoundsException();
  128. Integer ret = list.get(idx);
  129. list.remove(idx);
  130. return ret;
  131. }
  132. public int count(int el) {
  133. int counter = 0;
  134. for(Integer curr : list){
  135. if( curr.equals(el) )
  136. counter++;
  137. }
  138. return counter;
  139. }
  140. public void removeDuplicates() {
  141. for(int i = list.size()-1 ; i > 0; i--){
  142. list.subList(0, i).remove( list.get(i) );
  143. }
  144. }
  145. public IntegerList addValue(int value) {
  146. Integer [] arr= new Integer [list.size()];
  147. for(int i = 0; i < list.size(); i++){
  148. arr[i] = list.get(i) + value;
  149. }
  150. return new IntegerList(arr);
  151. }
  152. public void shiftLeft(int idx, int k) {
  153. while( k >= list.size() ) k-=list.size();
  154. int pom = idx - k;
  155. if( pom < 0 ) pom+=list.size();
  156. Integer el = list.get(idx);
  157. list.remove(idx);
  158. list.add(pom, el);
  159. }
  160. public void shiftRight(int idx, int k) {
  161. while( k >= list.size() ) k-=list.size();
  162. int pom = idx + k;
  163. if( pom >= list.size() ) pom-=list.size();
  164. Integer el = list.get(idx);
  165. list.remove(idx);
  166. list.add(pom, el);
  167. }
  168. public int sumFirst(int idx) {
  169. if( idx > list.size() ) idx = list.size();
  170. int sum = 0;
  171. for(int i = 0; i < idx; i++){
  172. sum += list.get(i);
  173. }
  174. return sum;
  175. }
  176. public int sumLast(int idx) {
  177. if( idx > list.size() ) idx = list.size();
  178. int sum = 0;
  179. for(int i = 0; i < idx; i++){
  180. sum += list.get( list.size() - 1 - i );
  181. }
  182. return sum;
  183. }
  184. public int size() {
  185. return list.size();
  186. }
  187. public int get(int idx) {
  188. return list.get(idx);
  189. }
  190. public String toString(){
  191. StringBuilder sb = new StringBuilder();
  192. for(Integer curr : list){
  193. sb.append(curr.toString()+" ");
  194. }
  195. return sb.toString();
  196. }
  197. }
  198.  
  199. public class IntegerListTest {
  200.  
  201. public static void main(String[] args) throws Exception {
  202. Scanner jin = new Scanner(System.in);
  203. int k = jin.nextInt();
  204. if ( k == 0 ) { //test standard methods
  205. int subtest = jin.nextInt();
  206. if ( subtest == 0 ) {
  207. IntegerList list = new IntegerList();
  208. while ( true ) {
  209. int num = jin.nextInt();
  210. if ( num == 0 ) {
  211. list.add(jin.nextInt(), jin.nextInt());
  212. }
  213. if ( num == 1 ) {
  214. list.remove(jin.nextInt());
  215. }
  216. if ( num == 2 ) {
  217. print(list);
  218. }
  219. if ( num == 3 ) {
  220. break;
  221. }
  222. }
  223. }
  224. if ( subtest == 1 ) {
  225. int n = jin.nextInt();
  226. Integer a[] = new Integer[n];
  227. for ( int i = 0 ; i < n ; ++i ) {
  228. a[i] = jin.nextInt();
  229. }
  230. IntegerList list = new IntegerList(a);
  231. print(list);
  232. }
  233. }
  234. if ( k == 1 ) { //test count,remove duplicates, addValue
  235. int n = jin.nextInt();
  236. Integer a[] = new Integer[n];
  237. for ( int i = 0 ; i < n ; ++i ) {
  238. a[i] = jin.nextInt();
  239. }
  240. IntegerList list = new IntegerList(a);
  241. while ( true ) {
  242. int num = jin.nextInt();
  243. if ( num == 0 ) { //count
  244. System.out.println(list.count(jin.nextInt()));
  245. }
  246. if ( num == 1 ) {
  247. list.removeDuplicates();
  248. }
  249. if ( num == 2 ) {
  250. print(list.addValue(jin.nextInt()));
  251. }
  252. if ( num == 3 ) {
  253. list.add(jin.nextInt(), jin.nextInt());
  254. }
  255. if ( num == 4 ) {
  256. print(list);
  257. }
  258. if ( num == 5 ) {
  259. break;
  260. }
  261. }
  262. }
  263. if ( k == 2 ) { //test shiftRight, shiftLeft, sumFirst , sumLast
  264. int n = jin.nextInt();
  265. Integer a[] = new Integer[n];
  266. for ( int i = 0 ; i < n ; ++i ) {
  267. a[i] = jin.nextInt();
  268. }
  269. IntegerList list = new IntegerList(a);
  270. while ( true ) {
  271. int num = jin.nextInt();
  272. if ( num == 0 ) { //count
  273. list.shiftLeft(jin.nextInt(), jin.nextInt());
  274. }
  275. if ( num == 1 ) {
  276. list.shiftRight(jin.nextInt(), jin.nextInt());
  277. }
  278. if ( num == 2 ) {
  279. System.out.println(list.sumFirst(jin.nextInt()));
  280. }
  281. if ( num == 3 ) {
  282. System.out.println(list.sumLast(jin.nextInt()));
  283. }
  284. if ( num == 4 ) {
  285. print(list);
  286. }
  287. if ( num == 5 ) {
  288. break;
  289. }
  290. }
  291. }
  292. }
  293.  
  294. public static void print(IntegerList il) {
  295. if ( il.size() == 0 ) System.out.print("EMPTY");
  296. for ( int i = 0 ; i < il.size() ; ++i ) {
  297. if ( i > 0 ) System.out.print(" ");
  298. System.out.print(il.get(i));
  299. }
  300. System.out.println();
  301. }
  302.  
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement