Sunday, November 20, 2016

URI Solution 1237 Compare Substring in C++ language

URI Online Judge | 1237

Compare Substring

By TopCoder*  USA
Timelimit: 1
Find the longest common substring between the two informed Strings. The substring can be any part of the String, including the entire String. If there is no common substring, return 0. The search is case sensitive ('x' != 'X').

Input

The input contains several test cases. Each test case is composed by two lines that contains a string each. Both input Strings will contain between 1 and 50, inclusive, letters (a-z, A-Z), and/or spaces.

Output

The length of the longest common substring between the two Strings.
Sample InputSample Output
abcdef
cdofhij
TWO
FOUR
abracadabra
open
Hey This java is hot
Java is a new paradigm
2
1
0
7



URI Solution 1237 Compare Substring in C++ language

#include <iostream>
#include <cstring>
#include <iomanip>
#include <cmath>
 
#define FOR(i, n) for(int i = 0; i < (n); ++i)
 
using namespace std;
 
int count (char *str1, char *str2)
{
    int c, k, max = 0, n1 = strlen(str1), n2 = strlen(str2);
 
    FOR(i, n1)
    {
        FOR(j, n2)
        {
            if (str1[i] == str2[j]) {
                c = 0;
 
                for (k = 0; (k + j) < n2; k++)
                {
                    if (str1[k+i] != str2[k+j])
                        break;
                    c++;
                }
 
                if (c > max)
                    max = c;
            }
        }
    }
 
    return max;
}
 
int main(int argc, char const *argv[])
{
    char str1[101], str2[101];
 
    while (cin.getline(str1, 101))
    {
        cin.getline(str2, 101);
        cout << count(str1, str2) << '\n';
    }
 
  return 0;
}


Download the Main Code From DropBox (Click Skip if any add occur)

No comments:
Write comments

To know more about the problem, give us your valuable commment. We'll try to help you. Thanks

All rights reserved ©2016 -URI ONLINE JUDGE SOLUTION | Developed by Maniruzzaman Akash

© 2016 URI ONLINE JUDGE SOLUTION. Developed by Maniruzzaman Akash | Distributed By Gooyaabi Templates
Powered by Blogger.