Guest User

Untitled

a guest
Apr 19th, 2020
599
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.78 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3.  
  4. public class Solution {
  5.  
  6. public static final long M = 1000000000;
  7.  
  8. public static void main(String[] args) {
  9. Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
  10. int t = s.nextInt();
  11. for(int ti=1;ti<=t;ti++)
  12. {
  13. String str = s.next();
  14. Stack<Pair> dis = new Stack<>();
  15. Stack<Integer> fact = new Stack<>();
  16. Pair d = new Pair(0, 0);
  17. int f = 1;
  18. for(int i=0;i<str.length();i++) {
  19. if(str.charAt(i)>='1' && str.charAt(i)<='9') {
  20. dis.push(d);
  21. fact.push(f);
  22.  
  23. f = str.charAt(i)-'0';
  24. d = new Pair(0, 0);
  25. i++;
  26. }
  27. else if(str.charAt(i)==')') {
  28. d.first = (d.first*f)%M;
  29. d.second = (d.second*f)%M;
  30. Pair dtillnow = dis.pop();
  31. dtillnow.first = (dtillnow.first+d.first)%M;
  32. dtillnow.second = (dtillnow.second+d.second)%M;
  33.  
  34. d = dtillnow;
  35. f = fact.pop();
  36. }
  37. else {
  38. char cc = str.charAt(i);
  39. if(cc=='N') {
  40. d.first--;
  41. }
  42. else if(cc=='S') {
  43. d.first++;
  44. }
  45. else if(cc=='E') {
  46. d.second++;
  47. }
  48. else {
  49. d.second--;
  50. }
  51.  
  52. d.first %= M;
  53. d.second %= M;
  54. }
  55. }
  56.  
  57. Pair ans = d;
  58. d.first += 1;
  59. d.second += 1;
  60.  
  61. if(d.first<=0) {
  62. d.first = M + d.first;
  63. }
  64. if(d.second<=0) {
  65. d.second = M + d.second;
  66. }
  67.  
  68. System.out.println("Case #"+ti+": "+d.second+" "+d.first);
  69. }
  70. }
  71.  
  72. static class Pair implements Comparable<Pair>{
  73. long first;
  74. long second;
  75.  
  76. Pair(long first, long second)
  77. {
  78. this.first = first;
  79. this.second = second;
  80. }
  81.  
  82. Pair() {}
  83.  
  84. public int compareTo(Pair other)
  85. {
  86. if(this.first<other.first)
  87. return -1;
  88. else
  89. return 1;
  90. }
  91. }
  92. }
Add Comment
Please, Sign In to add comment