javanetbeans's blog

By javanetbeans, 12 years ago, In English

Today I was going through SRM-556 DIV-2 500 question.

I tried to implement the solution in Java. It didn't work. Then implemented in C++. It worked. In Java I got stackoverflow error. While in C++, it ran perfectly.

Please refer this post

For the last test cases, the number of recursive calls made is 25600. Doesn't Java have enough space to accomodate this much instances (copies) of program.

Please help me why I am getting such strange behavior in Java????

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it

By javanetbeans, 12 years ago, In English

Today I was trying this code in C++:

string s1 = "a";

string s2 = "bcdef";

cout << s1.size() << " " << s2.size() << endl; //output 1 5

cout << (s1.size() — s2.size()) << endl; // output 4294967292

if I do,

cout << (int)(s1.size() — s2.size()) << endl; // then output -4.

Is this due to reason that s1.size() returns unsigned int.

Can you please exactly tell me, what's the funda for this?

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

By javanetbeans, 13 years ago, In English

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.io.PrintWriter;
  5. import java.math.*;
  6. import java.util.*;
  7. import java.io.*;

  8. public class C implements Runnable
  9. {
  10.     private void solve() throws IOException
  11.     {
  12.         String str = reader.readLine();
  13.         boolean male = false;
  14.         boolean female = false;
  15.         String[] ans = str.split(" ");
  16.         if(ans[0].endsWith("lios") || ans[0].endsWith("etr"))
  17.         {
  18.             male = true;
  19.         }
  20.         if(ans[0].endsWith("liala") || ans[0].endsWith("etra"))
  21.         {
  22.             female = true;
  23.         }
  24.         if((male == false && female == false) || (male && female))
  25.         {
  26.             System.out.println("NO");
  27.             return;
  28.         }
  29.        
  30.         if(ans.length == 1)
  31.         {
  32.             System.out.println("YES");
  33.             return;
  34.         }
  35.        
  36.         boolean adjective = false;
  37.         boolean noun = false;
  38.         boolean verbs = false;
  39.         int cnt = 0;
  40.         if(male == true)
  41.         {
  42.             for(int i = 0; i < ans.length; i++)
  43.             {
  44.                 if(ans[i].endsWith("lios") && noun == false)
  45.                 {
  46.                     adjective = true;
  47.                 }
  48.                 else if(ans[i].endsWith("etr") && cnt == 0)
  49.                 {
  50.                     noun = true;
  51.                     cnt++;
  52.                 }
  53.                 else if(ans[i].endsWith("initis") && noun && cnt == 1)
  54.                 {
  55.                     verbs = true;
  56.                 }
  57.                 else
  58.                 {
  59.                     System.out.println("NO");
  60.                     return;
  61.                 }
  62.             }
  63.             if(cnt == 1)
  64.                 System.out.println("YES");
  65.             else
  66.                 System.out.println("NO");
  67.         }
  68.         else if(female == true)
  69.         {
  70.             for(int i = 0; i < ans.length; i++)
  71.             {
  72.                 if(ans[i].endsWith("liala") && noun == false)
  73.                 {
  74.                     adjective = true;
  75.                 }
  76.                 else if(ans[i].endsWith("etra") && cnt == 0)
  77.                 {
  78.                     noun = true;
  79.                     cnt++;
  80.                 }
  81.                 else if(ans[i].endsWith("inites") && noun && cnt == 1)
  82.                 {
  83.                     verbs = true;
  84.                 }
  85.                 else
  86.                 {
  87.                     System.out.println("NO");
  88.                     return;
  89.                 }
  90.             }
  91.             if(cnt == 1)
  92.                 System.out.println("YES");
  93.             else
  94.                 System.out.println("NO");
  95.         }
  96.     }
  97.    
  98.     public static void main(String[] args)
  99.     {
  100.         new C().run();
  101.     }

  102.     BufferedReader reader;
  103.     StringTokenizer tokenizer;
  104.     PrintWriter writer;

  105.     public void run()
  106.     {
  107.         try
  108.         {
  109.             reader = new BufferedReader(new InputStreamReader(System.in));
  110.             tokenizer = null;
  111.             writer = new PrintWriter(System.out);
  112.             solve();
  113.             reader.close();
  114.             writer.close();
  115.         }
  116.         catch (Exception e)
  117.         {
  118.             e.printStackTrace();
  119.             System.exit(1);
  120.         }
  121.     }

  122.     int nextInt() throws IOException
  123.     {
  124.         return Integer.parseInt(nextToken());
  125.     }

  126.     long nextLong() throws IOException
  127.     {
  128.         return Long.parseLong(nextToken());
  129.     }

  130.     double nextDouble() throws IOException
  131.     {
  132.         return Double.parseDouble(nextToken());
  133.     }

  134.     String nextToken() throws IOException
  135.     {
  136.         while (tokenizer == null || !tokenizer.hasMoreTokens())
  137.         {
  138.             tokenizer = new StringTokenizer(reader.readLine());
  139.         }
  140.         return tokenizer.nextToken();
  141.     }
  142. }

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it