您的位置:首页 > 其它

zoj_1170

2011-10-03 01:36 113 查看
嗯嗯,开csdn博以后写的第三篇博文。。今天的任务勉强算完成了吧。。不知道明天能不能早点起床呢~

发现JOHN_BUG的开博提议很不错呵~

/*
zoj_1170    字符串处理
简单题。一开始以为是要匹配出最长的连续公共子序列。写完后才发现不用那么复杂。。
方法:记录两个字符串开始匹配的首位置就行了。然后一个一个匹配就好了。
*/
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
string a,b;

int check( int i,int j )
{
int len,len1,len2;
len1=a.size();
len2=b.size();
len=0;
while( i<len1 && j<len2 )
{
if( a[i]==b[j] )
{
len++;
}
i++;
j++;
}
return len;
}

void output( int up,int down )
{
int t1,t2,temp;
if( up==0 )
{
cout<<"appx("<<a<<","<<b<<") = 0\n";
return;
}
if( up==down )
{
cout<<"appx("<<a<<","<<b<<") = 1\n";
return;
}
if( up>down )
{
t1=up;
t2=down;
}
else
{
t1=down;
t2=up;
}
while( t2!=0 )  //求最大公约数喽
{
temp=t1%t2;
t1=t2;
t2=temp;
}
up/=t1;
down/=t1;
cout<<"appx("<<a<<","<<b<<") = "<<up<<"/"<<down<<endl;
}

int main()
{
int maxi,t,i;
while( cin>>a && a!="-1" )
{
cin>>b;
maxi=-1;
for( i=0;i<b.size();i++ )   //两个字符串首位置的选择
{
if( maxi<( t=check( 0,i ) ) )
maxi=t;
}
for( i=1;i<a.size();i++ )
{
if( maxi<( t=check( i,0 ) ) )
maxi=t;
}
output( 2*maxi,a.size()+b.size() );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: