Posted: Tue Jun 06, 2006 5:54 am
thanks, but i still not found any error.
Code: Select all
Removed. I found my fault
Code: Select all
#include <stdio.h>
#include <string.h>
removed after AC
Mushfiqur Rahman wrote:I yet not found any problem with my code, but it still getting WA.
I used here normal LCS with DP. It's gives me correct answer.
I have a question about the problem.
-> Would there be any character other than lower case (a...z).?
I am giving my code, please check it and let me know where's the trouble
Code: Select all
#include<stdio.h>
#include<string.h>
#include<ctype.h>
#define MAX 100
char str1[MAX][MAX];
char str2[MAX][MAX];
char* lower(char s[MAX])
{
char temp[MAX];
int i;
for(i=0;s[i];i++)
{
temp[i]=tolower(s[i]);
}
temp[i]='\0';
return temp;
}
int strlen(char s[MAX])
{
int len;
for(len=0;s[len];len++)
;
return len;
}
void print_LCS(int b[MAX][MAX], char x[MAX], int i, int j)
{
if(i==0 || j==0)
return;
if(b[i][j]==3)
{
print_LCS(b,x,i-1,j-1);
printf("%s ",str1[i-1]);
}
else if(b[i][j]==2)
print_LCS(b,x,i-1,j);
else
print_LCS(b,x,i,j-1);
}
void lcs(char x[MAX], char y[MAX])
{
int m,n;
int i,j;
int c[MAX][MAX];
int b[MAX][MAX];
m=strlen(x);
n=strlen(y);
for(i=0;i<=m;i++)
{
c[i][0]=0;
b[i][0]=0;
}
for(j=0;j<=n;j++)
{
c[0][j]=0;
b[0][j]=0;
}
for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
if(x[i-1]==y[j-1] && (strcmp(str1[i-1],str2[j-1])==0) )
{
c[i][j]=c[i-1][j-1]+1;
b[i][j]=3;
}
else if(c[i-1][j]>=c[i][j-1])
{
c[i][j]=c[i-1][j];
b[i][j]=2;
}
else
{
c[i][j]=c[i][j-1];
b[i][j]=1;
}
}
}
print_LCS(b,x,m,n);
}
int main()
{
char x[MAX],y[MAX];
char a[MAX],b[MAX];
int len;
int i;
int cases;
cases = 0;
freopen("5.cpp","r",stdin);
while(1)
{
if(scanf("%s",a)!=1)
break;
for(i=0;i<MAX;i++)
{
strcpy(str1[i],"");
strcpy(str2[i],"");
}
if(cases)printf("\n");
cases++;
i=0;
while(strcmp(a,"#")!=0)
{
x[i]=tolower(a[0]);
strcpy(str1[i],a);
scanf("%s",a);
i++;
}
x[i]='\0';
i=0;
scanf("%s",b);
while(strcmp(b,"#")!=0)
{
y[i]=tolower(b[0]);
strcpy(str2[i],b);
scanf("%s",b);
i++;
}
y[i]='\0';
//gets(b);
lcs(x,y);
printf("\n");
}
return 0;
}
so changeEach text is given as a sequence of lower-case words, separated by whitespace, but with no punctuation
Code: Select all
x[i]=tolower(a[0]);
Code: Select all
x[i]=a[0];
Code: Select all
void lcs(char x[MAX], char y[MAX])
Code: Select all
if(x[i-1]==y[j-1] && (strcmp(str1[i-1],str2[j-1])==0) )
Code: Select all
if( (strcmp(str1[i-1],str2[j-1])==0) )
Code: Select all
void print_LCS(int b[MAX][MAX], char x[MAX], int i, int j)
Code: Select all
if(cases)printf("\n");
Code: Select all
char* lower(char s[MAX])
Code: Select all
printf("%s ",str1[i-1]);
Code: Select all
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
typedef vector<string> VS;
string LCS(VS a, VS b)
{
int n = a.size();
int m = b.size();
string emo[n+1][m+1];
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
if (a[i-1] == b[j-1])
emo[i][j] = emo[i-1][j-1] + a[i-1] + " ";
else if (emo[i-1][j].size() < emo[i][j-1].size())
emo[i][j] = emo[i][j-1];
else
emo[i][j] = emo[i-1][j];
/*for (int i = 0; i <= n; i++)
{
for (int j = 0; j <= m; j++)
cout << emo[i][j] << " | ";
cout << endl;
}*/
return emo[n][m];
}
char modd(char x)
{
if (x >= 'A' && x <= 'Z')
return x-'A'+'a';
return x;
}
int main()
{
VS vv;
VS cc;
while (1)
{
string s;
while (1)
{
cin >> s;
if (s.empty())
goto end;
if (s == "#")
break;
transform(s.begin(), s.end(), s.begin(), modd);
vv.push_back(s);
}
while (1)
{
cin >> s;
if (s.empty())
goto end;
if (s == "#")
break;
transform(s.begin(), s.end(), s.begin(), modd);
cc.push_back(s);
}
s = LCS(vv, cc);
if (!s.empty())
cout << s.substr(0, s.size() - 1) << endl;
vv.clear();
cc.clear();
}
end:
return 0;
}
Code: Select all
int n = a.size();
int m = b.size();
string emo[n+1][m+1];
It doesn't compile on my Microsoft Visual Studio 2005 compiler. You are using non-constant identifiers for declaring arrays.-zx- wrote:indeed it does. you can compile it as c++.