各位大哥,小弟我有两到题实在是想不出,能不能帮帮忙(200分)

S

sunyi2k

Unregistered / Unconfirmed
GUEST, unregistred user!
(1)设有2个字符串a,b长度不超过40,且每个字符串中的字符不相同,找出2个字符串
中最长的公共部分,但是顺序不能到.例如:
a='abcdefghxyt'
b='xucdyhtkw'
最长的公共部分为'cdht'
问题:给出a,b之后,找出最长的公共部分.
(2)设有一个包含有n个顶点的多边形(n《=20),每个顶点的圆圈中有一个整数,表
示一个运算数。每个顶点之间的边上给出一个运算符。运算符仅有+,-,*,且无
优先级之分。
在多边形中,任意去掉一个边,此时可以进行计算,过程如下:
1)选取任意的2个相邻点和一个边进行计算
2)用运算的结果代替2个相邻的顶点和一个边。
重复上面的1,2两步,直到得到一个数时停止,称最后得到的数为结果。
程序要求:当n及多边形顶点中的数值以及边上运算符给出后,找出一个计算方案
(去掉一个边和计算顺序),使得到的结果为最大(约定全部计算在长整型范围
内)。
最好要用c语言写
 
1, 先比较长短,设 length(s1) <= Length(s2)
for i := Length(s1)do
wnto 1do
for j := 1 to Length(s1) - i + 1do
begin
s3 := copy(s1, j, i);
if Pos(s3, s2) > 0 then
begin
theAnswer := s3;
break;//退出循环。
end;
end;
2, 是什么样的?
1-- + -----------2
| |
| |
* |
| * 还是有可能 2,3 2,4 1,5
3 | 之间有运算符。
/ |
/ |
/ |
/4---- + ----5
 
第二题是着样的图
 
请大家帮忙,我有多少分就给多少。要这两题的原程序
 
>a='abcdefghxyt'
>b='xucdyhtkw'
>最长的公共部分为'cdht'
~~~~~~~~~~~~~~~~~~~~
看不懂题目。
 
1、
for i := 0 to len(s1)do

s3 := copy(s1, i, 1);
if Pos(s3, s2) > 0 then
begin
theAnswer := theAnswer + s3;
temp=pos(s3,s2)
s2:=copy(s2,temp+1,len(s2)-temp)
if len(s2)=0 then
break
end;
end;

2、
三个数组:(1)第一操作数f(2)运算符o(3)第二操作数s
循环:
max=0 //最大值
count=0 //删掉的边数
while count<len(o)
begin
for i=0 to len(f)-1
if o(i)<>"null" then
//为空表示此边已被删
begin
if max <= f(i) o(i) s(i) then
begin
max=f(i) o(i) s(i)
index=i
end
end
next i
tempf=f(index)
for i=0 to len(f)-1
if f(i)=tempf then
f(i)=max
next i
temps=s(index)
for i=0 to len(s)-1
if s(i)=temps then
s(i)=max
next i
o(index)="null"
count=count+1
end
此时任意一个操作数即为结果
 
上面应该是:while count不等于"null"
 
第一题说得不清楚
>>a='abcdefghxyt'
>>b='xucdyhtkw'
>>最长的公共部分为'cdht'
如果是
a='abcdefghxyt'
b='xducdyhtkw'

你说最长的公共部分是什么呢?
如果以a为源比较字串的话,最长公共部分为‘cdht'
如果以b为源比较字串的活,最长公共部分为‘dht'
源代码不难做,关键是以何种方式算出最长公共部分
 
to wuwo
不管用那串应该都是cdht
 
快点,我也要原程序,好难
 
程序已经发到你的信箱,请查收。
 
多人接受答案了。
 

Similar threads

回复
0
查看
860
不得闲
S
回复
0
查看
3K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
2K
SUNSTONE的Delphi笔记
S
D
回复
0
查看
903
DelphiTeacher的专栏
D
D
回复
0
查看
889
DelphiTeacher的专栏
D
顶部