Re: 10100 - Longest Match
Posted: Tue Oct 05, 2010 5:42 pm
my code also gives the same output.... but why wa..........
Code: Select all
hello world
HELLO
Code: Select all
#include <iostream>
#include <string>
#include <stdio.h>
#include <math.h>
#include <cmath>
#include <iomanip>
#include <ctype.h>
#include <string.h>
using namespace std;
string arr1[1002];
string arr2[1002];
int arrDef[1002][1002];
int procesarLinea (string a, string arr[1002]);
int main()
{
int cont = 1;
string a, b;
while (getline(cin, a))
{
memset(arrDef, 0, sizeof arrDef);
memset(arr1, 0, sizeof arr1);
memset(arr2, 0, sizeof arr2);
int numPalabras1, numPalabras2;
getline(cin, b);
if (a.size() == 0 || b.size() == 0)
{
cout << cont << ". Blank!" << endl;
}
else
{
int numLetras = 0;
string::iterator it = a.begin();
for (int i = 0; i < a.size(); i++)
{
if (numLetras == 0 && a[i] == ' ')
{
a.erase(it);
i=-1;
}
else if (isalnum(a[i]))
{
numLetras++;
it++;
}
else if (numLetras > 0 && a[i] == ' ' && i < a.size()-1 && a[i+1] == ' ')
{
a.erase(it);
i--;
}
else it++;
}
numLetras = 0;
it = b.begin();
for (int i = 0; i < b.size(); i++)
{
if (numLetras == 0 && b[i] == ' ')
{
b.erase(it);
i = -1;
}
else if (isalnum(b[i]))
{
numLetras++;
it++;
}
else if (numLetras > 0 && b[i] == ' ' && i < b.size()-1 && b[i+1] == ' ')
{
b.erase(it);
i--;
}
else it++;
}
numPalabras1 = procesarLinea(a, arr1);
numPalabras2 = procesarLinea(b, arr2);
for (int i = 1; i <= numPalabras1; i++)
{
for (int j = 1; j <= numPalabras2; j++)
{
if (arr1[i-1] == arr2[j-1])
{
arrDef[i][j] = arrDef[i-1][j-1] + 1;
}
else
{
arrDef[i][j] = max (arrDef[i][j-1], arrDef[i-1][j]);
}
}
}
cout << cont << ". Length of longest match:";
cout << setw(2);
cout << arrDef[numPalabras1][numPalabras2] << endl;
}
cont++;
}
return 0;
}
int procesarLinea (string a, string arr[1002])
{
int numPalabras = 1;
int numPalabrasDef = 0;
for (int i = 0; i < a.size(); i++)
{
if (a[i] >= 'A' && a[i] <= 'Z')
{
numPalabrasDef = numPalabras;
a[i] = tolower(a[i]);
arr[numPalabras-1] = arr[numPalabras-1] + a[i];
}
else if (isalnum(a[i]))
{
numPalabrasDef = numPalabras;
arr[numPalabras-1] = arr[numPalabras-1] + a[i];
}
else
{
numPalabras++;
}
}
return numPalabrasDef;
}
Code: Select all
1. Length of longest match: 1
2. Blank!
3. Length of longest match: 2
Code: Select all
1. Length of longest match: 1
2. Blank!
3. Length of longest match: 2
Code: Select all
This is a test.
test
Hello!
The document provides late-breaking information
late breaking.
This is a test.
test
Hello!
The document provides late-breaking information
late breaking.
This is a test.
test
Hello!
The document provides late-breaking information
late breaking.
This is a test.
test
Hello!
The document provides late-breaking information
late breaking.
Code: Select all
1. Length of longest match: 1
2. Blank!
3. Length of longest match: 2
4. Length of longest match: 1
5. Blank!
6. Length of longest match: 2
7. Length of longest match: 1
8. Blank!
9. Length of longest match: 2
10. Length of longest match: 1
11. Blank!
12. Length of longest match: 2
Code: Select all
thank you brianfry713
I guess we are counting words which gets matches, even uDebug gives output 1.brianfry713 wrote:Input:Output should be 2.a!aa!aaa!
aa
Code: Select all
The document provides late-breaking information
late breaking.
aaa a a a a
a
Code: Select all
1. Length of longest match: 2
2. Length of longest match: 1