Wednesday, November 30, 2011

A program to print the longest common substring of the two given strings (Java)

//This Java program accepts two strings
//from the user and then prints the longest common
//substring of the two given strings.
//SUBSTRING: A substring of a string is a string that can
//be obtained by removing some characters from
//the beginning or the end of the given string. For example
// "uter" is a substring of "computer".
//
//Sample Output of program :-
/*
Enter the two strings :
i like computer
put it down

Longest Common Substring is :-
put
*/

import java.util.*;
class longestCommonSubstring
{
public static void main()
{
    System.out.println("Enter the two strings : ");
    String a,b;
    Scanner sc=new Scanner(System.in);
    a=sc.nextLine();
    b=sc.nextLine();
    int m=a.length();
    int n=b.length();
    int ar[][]=new int[m+1][n+1];
    int i;
    int j;
    for(i=0;i<m+1;i++)   
    for(j=0;j<n+1;j++)
    ar[i][j]=0;
   
   
    int l=0,x=0,y=0;
    for(i=1;i<m+1;i++)
    {
        for(j=1;j<n+1;j++)
        {
            if(a.charAt(i-1)==b.charAt(j-1))
            {
                ar[i][j]=ar[i-1][j-1]+1;
                if(ar[i][j]>=l)
                {
                    l=ar[i][j];  //l holds value of the largest number in the square which
                    x=i;         //is also the length of longest substring.x and y store its
                    y=j;         //co-ordinates.
                }
            }
        }
    }
   
    String lcs="";
    int k=0;
    for(i=l;i>=1;i--)     //Storing the largest common substring
    {
        if(ar[x][y]!=0)
        {
            lcs=a.charAt(x-1)+lcs;
            x--;y--;
        }
    }
    System.out.println("\nLongest Common Substring is :-");
    System.out.println(lcs);          //Printing the largest common substring
}
}

//Author : Mayank Rajoria

A Java program to find the length of the longest non decreasing subsequence from a group of integers (Java)

//This Java program first accepts a number from the user
//and then accepts that many integers. The program then, using
//dynamic programming, finds the length of the longest
//subsequence of integers such that they are not in
//decreasing order.
//SEQUENCE : A subsequence is a sequence that can
//be derived from another sequence by deleting some
//elements without changing the order of the remaining
//elements. For example, the sequence <1,4,6> is a
//subsequence of <2,1,5,6,4,7,6>.

//Sample Output :-
/*Enter number of integers : 8
Enter the integers :-

-2
5
3
4
10
7
9
12

Longest non-decreasing sequence is of length : 6
*/

import java.util.*;
class longestNonDecreasingSequence
{
public static void main()
{
    int n;
    System.out.print("Enter number of integers : ");
    Scanner sc=new Scanner(System.in);
    n=sc.nextInt();
    int a[]=new int[n];
    int i;
    System.out.println("Enter the integers :-\n");
    for(i=0;i<n;i++)a[i]=sc.nextInt();
    int s[]=new int[n];
    for(i=0;i<n;i++)s[i]=1;
   
    int j,k=0;
    for(i=0;i<n;i++)
    {
        for(j=0;j<i;j++)
        {
            if(a[i]>=a[j])
            {
                k=s[j]+1;
                if(s[i]<=k)
                {
                    s[i]=k;
                }
            }
        }
    }
    int ans=a[0];;
    for(i=0;i<n;i++)
    {
        if(s[i]>=ans)
        {
            ans=s[i];
        }
    }
    System.out.print("\nLongest non-decreasing sequence is of length : "+ans);
}
}

//Author : Mayank Rajoria

Sunday, November 20, 2011

Printing a pyramid pattern of digits #12

//This java program accepts a number of user
//which tells the program that how many lines
//of the pattern will be printed.
/*Sample Input :6
Sample Output :
                0
             1 0 1
          2 1 0 1 2
       3 2 1 0 1 2 3
    4 3 2 1 0 1 2 3 4
 5 4 3 2 1 0 1 2 3 4 5   */

import java.util.*;
class pattern
{
public static void main()
{
    System.out.println("Enter a number");
    Scanner sc=new Scanner(System.in);
    int n=sc.nextInt();
    int i,j;
    for(i=0;i<=n;i++)
    {
        for(j=0;j<=(2*(n-i));j++)
        {
            System.out.print(" ");
        }
       
        for(j=i;j>=0;j--)
        {
            System.out.print(j+" ");
        }
       
        for(j=1;j<=i;j++)
        {
            System.out.print(j+" ");
        }
        System.out.println();
    }
}
}

//Author : Mayank Rajoria

Saturday, November 12, 2011

Using Selection Sort to sort an array (Java)

//This Java program depicts the use of Selection
//sort technique to arrange the integers in
//an array in ascending order.

import java.util.*;
class selectionSort
{
static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 10");
    int a[]=new int[10];
    int i;
    for(i=0;i<10;i++)
    {
        a[i]=sc.nextInt();
    }
    int min=0,j,k;

    for(i=0;i<10;i++)
    {
        for(j=i;j<10;j++)
        {
            if(a[i]>a[j])
            {
                k=a[i];
                a[i]=a[j];
                a[j]=k;
            }
        }
    }

    for(i=0;i<10;i++)
    {
        System.out.println(a[i]);
    }
}
}

//Author : Mayank Rajoria

Using Bubble Sort for an array (Java)

//This Java program depicts the use of Bubble
//sort technique to arrange the integers in
//an array in ascending order.

import java.util.*;
class bubbleSort
{
static void main()
{
    Scanner sc=new Scanner(System.in);
    System.out.println("Enter 10 numbers :-");
    int a[]=new int[10];
    int i;
    for(i=0;i<10;i++)
    {
        a[i]=sc.nextInt();   //Accepting numbers
    }
    int min=0,j,k;

    for(i=0;i<10;i++)
    {
        for(j=0;j<9-i;j++)
        {
            if(a[j]>a[j+1])
            {
                k=a[j];           
                a[j]=a[j+1];   
                a[j+1]=k;      
            }
        }
    }

    for(i=0;i<10;i++)
    {
        System.out.println(a[i]); //Printing the numbers
    }
}
}

//Author : Mayank Rajoria