Monday, February 9, 2015

FrequencyCount.java in pgm



FrequencyCount.java

public class FrequencyCount
{
            public static void main(String[] args)
             {
                    String s = StdIn.readAll();
         s = s.toLowerCase();
        s = s.replaceAll("[\",!.:;?()']", "");
        String[] words = s.split("\\s+");

        Merge.sort(words);

        Counter[] zipf = new Counter[words.length];
        int M = 0;                                       
        for (int i = 0; i < words.length; i++)
       {
            if (i == 0 || !words[i].equals(words[i-1]))  
                zipf[M++] = new Counter(words[i], words.length);
            zipf[M-1].increment();
        }

        Merge.sort(zipf, 0, M);                           
        for (int j = M-1; j >= 0; j--)
       {
            StdOut.println(zipf[j]);
        }
    }
}
import java.util.Arrays;
 
public class LRS 
{
               public static String lcp(String s, String t) 
              {
        int n = Math.min(s.length(), t.length());
        for (int i = 0; i < n; i++) 
 {
            if (s.charAt(i) != t.charAt(i))
                return s.substring(0, i);
        }
        return s.substring(0, n);
    }
    public static String lrs(String s) 
   {
        int N  = s.length();
        String[] suffixes = new String[N];
        for (int i = 0; i < N; i++) 
       {
            suffixes[i] = s.substring(i, N);
        }
        Arrays.sort(suffixes);
        String lrs = "";
        for (int i = 0; i < N - 1; i++) 
        {
            String x = lcp(suffixes[i], suffixes[i+1]);
            if (x.length() > lrs.length())
                lrs = x;
        }
        return lrs;
    }
    public static void main(String[] args) 
   {
        String s = StdIn.readAll();
        s = s.replaceAll("\\s+", " ");
        StdOut.println("'" + lrs(s) + "'");
    }
}

No comments:

Post a Comment