CodeChef is a non-commercial competitive programming community
Login
Username (New User? Signup) Password (Forgot Password?)
Signup
Login or
Signup with
Connect
Note
  • Publicize your achievements on your Facebook Wall.
  • Challenge your friends or ask them for help.

Site Navigation

  • PRACTICE
    • Easy
    • Medium
    • Hard
    • Challenge
    • Peer
  • COMPETE
    • February CookOff
    • February Long Contest
    • January CookOff
  • DISCUSS
    • Wiki
    • Forums
    • Blog
    • Twitter
  • COMMUNITY
    • CodeChef Meetups
    • Campus Chapters
    • Host your Contest
    • User Groups
    • CodeChef TechTalks
    • All Educational Initiatives
    • Event Calendar
  • HELP
    • Frequently Asked Questions
    • FAQ for problem setters
    • Problem Setting
    • Ranks
    • Tutorials
  • ABOUT
    • About CodeChef
    • Team CodeChef
    • Press Room
    • CodeChef Financials
    • CodeChef Sponsorships
    • CEO's Corner
    • Contact Us
    • About Directi
Home  » November 2009 (Contest X) » The LCS Problem Revisited » All Submissions » Victor Padilla [131038]
0
Your rating: None
CodeChef submission 131038 (JAVA)

CodeChef submission 131038 (JAVA) plaintext list. Status: TLE, problem J2, contest NOV09. By vector9x (vector9x), 2009-11-11 14:42:22.
  1. import java.io.*;
  2. import java.util.*;
  3. import java.awt.Point;
  4.  
  5. public class Main {
  6. static BufferedReader input;
  7. public static void main(String[] args) throws IOException
  8. {
  9. Locale.setDefault(Locale.US);
  10. input=new BufferedReader(new InputStreamReader(System.in));
  11. //input=new BufferedReader(new FileReader("input.txt"));
  12. int numCasos = Integer.parseInt(input.readLine());
  13. for(int caso=0; caso<numCasos; caso++)
  14. {
  15. //long ini = System.currentTimeMillis();
  16. A=input.readLine();
  17. B=input.readLine();
  18. input.readLine();
  19. /*
  20. String corta, larga;
  21. if(A.length()<B.length()) {
  22. corta = A;
  23. larga = B;
  24. }
  25. else {
  26. corta = B;
  27. larga = A;
  28. }
  29. if(larga.startsWith(corta) || larga.endsWith(corta)) {
  30. System.out.println(corta.length()+" 1");
  31. continue;
  32. }
  33. */
  34. solve();
  35. //System.err.println("time "+(System.currentTimeMillis()-ini)/1000.0);
  36. }
  37. }
  38. static String A, B;
  39. static short[][] lcs=new short[1001][1001];
  40. static boolean[][] pivot=new boolean[1001][1001];
  41. //static byte[][] prev=new byte[1001][1001];
  42. static final int MOD = 23102009;
  43. static final byte IZQ='<', ARR='^', DIAG='\\', AMBOS='+';
  44. static PairSet[][] from=new PairSet[1001][1001];
  45. static {
  46. from[0][0]=new PairSet();
  47. for(int i=1;i<1001;i++){
  48. from[i][0]=from[0][0];
  49. from[0][i]=from[0][0];
  50. }
  51. }
  52. static char A_(int idx) {
  53. return A.charAt(idx-1);
  54. }
  55. static char B_(int idx) {
  56. return B.charAt(idx-1);
  57. }
  58. static void solve()
  59. {
  60. for(int i=1; i<=A.length(); i++) {
  61. for(int j=1; j<=B.length(); j++) {
  62. if(A_(i)==B_(j)) {
  63. lcs[i][j] = (short)(lcs[i-1][j-1] + 1);
  64. pivot[i][j]=true;
  65. //prev[i][j]=DIAG;
  66. if(pivot[i-1][j-1]) {
  67. from[i][j]=new PairSet();
  68. from[i][j].add(new Point(i-1,j-1));
  69. }
  70. else {
  71. from[i][j]=from[i-1][j-1];
  72. }
  73. }
  74. else {
  75. if(lcs[i-1][j]>lcs[i][j-1]) {
  76. lcs[i][j] = lcs[i-1][j];
  77. //prev[i][j] = ARR;
  78. if(pivot[i-1][j]) {
  79. from[i][j]=new PairSet();
  80. from[i][j].add(new Point(i-1,j));
  81. } else {
  82. from[i][j]=from[i-1][j];
  83. }
  84. }
  85. else if(lcs[i][j-1]>lcs[i-1][j]) {
  86. lcs[i][j] = lcs[i][j-1];
  87. //prev[i][j] = IZQ;
  88. if(pivot[i][j-1]) {
  89. from[i][j]=new PairSet();
  90. from[i][j].add(new Point(i,j-1));
  91. } else {
  92. from[i][j]=from[i][j-1];
  93. }
  94. }
  95. else {
  96. lcs[i][j] = lcs[i-1][j];
  97. //prev[i][j] = AMBOS;
  98. from[i][j]=new PairSet();
  99. from[i][j].addAll(from[i-1][j]);
  100. from[i][j].addAll(from[i][j-1]);
  101. }
  102. pivot[i][j] = false;
  103. }
  104. }
  105. }
  106. //printLCS();
  107. for(int i=1;i<=A.length(); i++) {
  108. Arrays.fill(memo[i],1,B.length()+1, -1);
  109. //Arrays.fill(memoAnts[i],1,B.length()+1, null);
  110. }
  111. int total=0;
  112.  
  113. total = go(A.length(), B.length());
  114. System.out.println(lcs[A.length()][B.length()]+" "+total);
  115. }
  116.  
  117. static int[][] memo=new int[1002][1002];
  118. static int go(int ipiv, int jpiv /*, int deep*/)
  119. {
  120. //print(deep,"go("+ipiv+","+jpiv+") "+(char)prev[ipiv][jpiv]);
  121. if(memo[ipiv][jpiv]!=-1) {
  122. //print(deep, "memo! "+ipiv+","+jpiv);
  123. return memo[ipiv][jpiv];
  124. }
  125. if(pivot[ipiv][jpiv] && lcs[ipiv][jpiv]==1) {
  126. return memo[ipiv][jpiv]=1;
  127. }
  128. int res = 0;
  129. Arrays.fill(usadoFila, 1, ipiv+1, false);
  130. Arrays.fill(usadoCol, 1, jpiv+1, false);
  131. for(Point piv: from[ipiv][jpiv]) {
  132. //print(deep,"["+piv.x+","+piv.y+"]");
  133. if(!usadoFila[piv.x] && !usadoCol[piv.y]) {
  134. res += go(piv.x,piv.y);
  135. res %= MOD;
  136. usadoFila[piv.x]=true;
  137. usadoCol[piv.y]=true;
  138. }
  139. }
  140. return memo[ipiv][jpiv]=res;
  141. }
  142. static int __go(int ipiv, int jpiv /*, int deep*/)
  143. {
  144. //print(deep,"go("+ipiv+","+jpiv+") "+(char)prev[ipiv][jpiv]);
  145. if(memo[ipiv][jpiv]!=-1) {
  146. //print(deep, "memo! "+ipiv+","+jpiv);
  147. return memo[ipiv][jpiv];
  148. }
  149. if(pivot[ipiv][jpiv] && lcs[ipiv][jpiv]==1) {
  150. return memo[ipiv][jpiv]=1;
  151. }
  152. int res = 0;
  153. for(Point piv: pivsAnteriores(ipiv, jpiv)) {
  154. //print(deep,"["+piv.x+","+piv.y+"]");
  155. res += go(piv.x,piv.y);
  156. res %= MOD;
  157. }
  158. return memo[ipiv][jpiv]=res;
  159. }
  160. static boolean[][] visited=new boolean[1001][1001];
  161. static boolean[] usadoFila=new boolean[1001];
  162. static boolean[] usadoCol=new boolean[1001];
  163. //static PairSet[][] memoAnts=new PairSet[1001][1001];
  164. static PairSet pivsAnteriores(int i, int j) {
  165. //if(memoAnts[i][j]!=null) return memoAnts[i][j];
  166. //System.out.println("pivsAnteriores("+i+","+j);
  167. for(int k=0; k<=i; k++)
  168. Arrays.fill(visited[k],0,j+1, false);
  169. Arrays.fill(usadoFila, 1, i+1, false);
  170. Arrays.fill(usadoCol, 1, j+1, false);
  171. PairSet res = new PairSet();
  172. pivsAnterioresIter(res,i,j, true);
  173. //memoAnts[i][j]=res;
  174. return res;
  175. }
  176. static void pivsAnterioresIter(PairSet set, int i, int j, boolean first)
  177. {
  178. int mov;
  179. outer:
  180. for(;;) {
  181. if(visited[i][j]) return;
  182. visited[i][j]=true;
  183. //print(deep,"<"+i+","+j+">");
  184. if(!first && pivot[i][j]) {
  185. if(!usadoFila[i] && !usadoCol[j]) {
  186. set.add(new Point(i,j));
  187. //print(deep,"agregando "+i+","+j);
  188. usadoFila[i]=true;
  189. usadoCol[j]=true;
  190. }
  191. return;
  192. }
  193. first=false;
  194. mov=0;
  195. switch(mov)
  196. {
  197. case IZQ: j--; break;
  198. case ARR: i--; break;
  199. case DIAG: i--; j--; break;
  200. case AMBOS:
  201. pivsAnterioresIter(set,i-1,j,false);
  202. pivsAnterioresIter(set,i,j-1,false);
  203. break outer;
  204. default:
  205. //System.out.println(".");
  206. break outer;
  207. }
  208. }
  209. //System.out.println("))"
  210. }
  211. static void print(int deep, String str)
  212. {
  213. for(int i=0;i<deep;i++) System.err.print(" ");
  214. System.err.println(str);
  215. }
  216.  
  217. static void printLCS()
  218. {
  219. for(int i=1; i<=A.length(); i++) {
  220. for(int j=1; j<=B.length(); j++) {
  221. if(pivot[i][j])
  222. System.err.print("["+lcs[i][j]+"]");
  223. else
  224. System.err.print(" "+lcs[i][j]+" ");
  225. }
  226. System.err.println();
  227. }
  228. }
  229. static class PairSet extends HashSet<Point>{}
  230. }


Comments

  • Login or Register to post a comment.

CodeChef is a non-commercial competitive programming community
  • About CodeChef
  • About Directi
  • CEO's Corner
  • C-Programming
  • Programming Languages
  • Contact Us
© 2009 Directi Group. All Rights Reserved. CodeChef uses SPOJ © by Sphere Research Labs
In order to report copyright violations of any kind, send in an email to copyright@codechef.com
CodeChef a product of Directi
The time now is:
CodeChef - A Platform for Aspiring Programmers

CodeChef was created as a platform to help programmers make it big in the world of computer programming. At CodeChef we work hard to revive the geek in you by hosting programming contests on a monthly basis. We also aim to have training sessions and events related to online programming for programmers around the world. Apart from providing a platform for programming competitions, CodeChef also has various tutorials and forum discussions to help those who are new to the world of computer programming.

Practice Section - A Place to hone your 'Computer Programming Skills'

Try your hand at one of our many practice problems and submit your solution in a language of your choice. Our judge accepts solutions in over 35+ programming languages. Online programming was never this much fun! Receive points, and move up through the CodeChef ranks. Use our practice section to better prepare yourself for the multiple programming competitions that take place through-out the month on CodeChef.

Compete - Monthly Programming Contests and Cook-offs

Here is where you can show off your computer programming skills. Take part in our 10 day long monthly programming contests and the shorter format Cook-off programming contests. Put yourself up for recognition and win great prizes. Prizes worth up to Rs.20,000 and $700 are up for grabs every month along with lots more CodeChef goodies.

Discuss

Are you new to computer programming? Do you need help with algorithms? Then be part of CodeChefs Forums and interact with all our programmers love helping out other programmers and share their ideas.

CodeChef Community

As part of our Educational initiative, we give institutes the opportunity to associate with CodeChef in the form of Campus Chapters. Hosting online programming competitions is not the only feature on CodeChef. Be a part of the CodeChef community through CodeChef meetups and techtalks. You can also host a programming contest for your institute on CodeChef and be a guest author on our blog.

Domain Name Registration, Web hosting, and Website Design provided by BigRock.com