请高手优化一个VFP程序

weixin_41315215 2023-07-15 10:35:50

1组由0、1、2、3组成的长度为8字节的字符串集合(每个字符串都是唯一的,没有完全相同的),每个字符串一行,如何以最快速度找出每个字符串在集合中相同位置没有相同字符的行数?
比如,00000000这个字符串,相同位置没有相同字符的所有字符串是任何位置都没有0出现的,也就是全部由1、2、3组成,理论上有3^8=6561个;
11111111这个字符串,相同位置没有相同字符的所有字符串是任何位置都没有1出现的,也就是全部由0、2、3组成,理论上也有3^8=6561个;
22222222这个字符串,相同位置没有相同字符的所有字符串是任何位置都没有2出现的,也就是全部由0、1、3组成,理论上也有3^8=6561个;
33333333这个字符串,相同位置没有相同字符的所有字符串是任何位置都没有3出现的,也就是全部由0、1、2组成,理论上也有3^8=6561个;
如果要找每个字符串在集合中相同位置没有相同字符的行数,那么就需要每个字符串都要循环6561次去遍历查找,这样就耗费不少时间。
以下是用循环遍历的程序,请高手优化这个程序。(注:TEST=324.TXT只是1个集合的例子,实际应用中的集合有可能不止这个行数的。如果所有的字符串都包含的话,总行数是4^8=65536行。如果用SQL查找,行数少的时候看不出快慢,行数多的时候也会很慢的,SQL实际上也是遍历)
set talk off
file1=getfile([txt],[我的集合])
if file(file)
 sele 0
 crea temp3210 (xb c(1))
 insert into temp3210 values ([0])
 insert into temp3210 values ([1])
 insert into temp3210 values ([2])
 insert into temp3210 values ([3])
 sele 0
 crea cursor temp0 (jx c(8),f0 i)
 appe fields jx from [&file1] sdf
 index on jx tag jx
 go top
 scan
  x1=jx
  =aline(tt,strconv(x1,5),0h00)      &&&&将字符串x1的每个字符装入数组tt,然后生产所有相同位置没有相同字符的字符串
  select a1.xb+a2.xb+a3.xb+a4.xb+a5.xb+a6.xb+a7.xb+a8.xb as jx from ;
   temp3210 a1,;
   temp3210 a2,;
   temp3210 a3,;
   temp3210 a4,;
   temp3210 a5,;
   temp3210 a6,;
   temp3210 a7,;
   temp3210 a8 where ;
   !(a1.xb=tt(1) or a2.xb=tt(2) or a3.xb=tt(3) or a4.xb=tt(4) or ;
   a5.xb=tt(5) or a6.xb=tt(6) or a7.xb=tt(7) or a8.xb=tt(8)) into array myjx
  s1=_tally
  sele temp0
  for k1=1 to s1   &&&&&遍历查找
   seek myjx(k1)
   if found()
    v0=v0+1
   endif
  endfor
  seek x1    &&&&&回到原来位置
  replace f0 with v0
 endscan
endif
return


TEST=324.TXT
------------
33332233
33332211
33332200
33331133
33331122
33331100
33330033
33330022
33330011
33322333
33322233
33322211
33322200
33322111
33322000
33311333
33311222
33311133
33311122
33311100
33311000
33300333
33300222
33300111
33300033
33300022
33300011
33223333
33223322
33223311
33223300
33222333
33222233
33222211
33222200
33222111
33222000
33221133
33221122
33221111
33221100
33220033
33220022
33220011
33220000
33113333
33113322
33113311
33113300
33112233
33112222
33112211
33112200
33111333
33111222
33111133
33111122
33111100
33111000
33110033
33110022
33110011
33110000
33003333
33003322
33003311
33003300
33002233
33002222
33002211
33002200
33001133
33001122
33001111
33001100
33000333
33000222
33000111
33000033
33000022
33000011
22333322
22333311
22333300
22333222
22333111
22333000
22332233
22332222
22332211
22332200
22331133
22331122
22331111
22331100
22330033
22330022
22330011
22330000
22233322
22233311
22233300
22233222
22233111
22233000
22223322
22223311
22223300
22221133
22221122
22221100
22220033
22220022
22220011
22211333
22211222
22211133
22211122
22211100
22211000
22200333
22200222
22200111
22200033
22200022
22200011
22113333
22113322
22113311
22113300
22112233
22112222
22112211
22112200
22111333
22111222
22111133
22111122
22111100
22111000
22110033
22110022
22110011
22110000
22003333
22003322
22003311
22003300
22002233
22002222
22002211
22002200
22001133
22001122
22001111
22001100
22000333
22000222
22000111
22000033
22000022
22000011
11333322
11333311
11333300
11333222
11333111
11333000
11332233
11332222
11332211
11332200
11331133
11331122
11331111
11331100
11330033
11330022
11330011
11330000
11223333
11223322
11223311
11223300
11222333
11222233
11222211
11222200
11222111
11222000
11221133
11221122
11221111
11221100
11220033
11220022
11220011
11220000
11133322
11133311
11133300
11133222
11133111
11133000
11122333
11122233
11122211
11122200
11122111
11122000
11113322
11113311
11113300
11112233
11112211
11112200
11110033
11110022
11110011
11100333
11100222
11100111
11100033
11100022
11100011
11003333
11003322
11003311
11003300
11002233
11002222
11002211
11002200
11001133
11001122
11001111
11001100
11000333
11000222
11000111
11000033
11000022
11000011
00333322
00333311
00333300
00333222
00333111
00333000
00332233
00332222
00332211
00332200
00331133
00331122
00331111
00331100
00330033
00330022
00330011
00330000
00223333
00223322
00223311
00223300
00222333
00222233
00222211
00222200
00222111
00222000
00221133
00221122
00221111
00221100
00220033
00220022
00220011
00220000
00113333
00113322
00113311
00113300
00112233
00112222
00112211
00112200
00111333
00111222
00111133
00111122
00111100
00111000
00110033
00110022
00110011
00110000
00033322
00033311
00033300
00033222
00033111
00033000
00022333
00022233
00022211
00022200
00022111
00022000
00011333
00011222
00011133
00011122
00011100
00011000
00003322
00003311
00003300
00002233
00002211
00002200
00001133
00001122
00001100

 

...全文
541 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
sych11 2023-11-19
  • 打赏
  • 举报
回复

sec=SECONDS()
set talk off
CLOSE all
create cursor tmp (id n(1))
insert into tmp (id) value (0)
insert into tmp (id) value (1)
insert into tmp (id) value (2)
insert into tmp (id) value (3)
select a.id as id1,b.id as id2,c.id as id3,d.id as id4,e.id as id5,f.id as id6,g.id as id7,h.id as id8,cast(0 as integer) as id0 from tmp a , tmp b , tmp c , tmp d , tmp e, tmp f, tmp g, tmp h INTO CURSOR temp1
COPY TO temp2
*SELECT temp1
scan
SELECT COUNT(
) FROM temp2 WHERE temp1.id1<>temp2.id1 AND temp1.id2<>temp2.id2 AND temp1.id3<>temp2.id3 AND temp1.id4<>temp2.id4 AND temp1.id5<>temp2.id5 AND temp1.id6<>temp2.id6 AND temp1.id7<>temp2.id7 AND temp1.id8<>temp2.id8 INTO array temp3
?temp3

  • SELECT temp1
  • endscan
    ?SECONDS()-sec
    retu
sych11 2023-11-19
  • 打赏
  • 举报
回复

set talk off
CLOSE all
TEXT TO cTxT noshow
33332233
00000000
01201201
ENDTEXT
=STRTOFILE(ctxt,"ctxt.txt")
crea curs temp3210 (xb c(8))
APPEND FROM ctxt.txt TYPE DELIMITED
ERASE ctxt.txt
DIMENSION ctxt(8,4)
ctxt=0
SCAN
FOR i=1 TO 8
ctxt(i,VAL(SUBSTR(xb,i,1))+1)=ctxt(i,VAL(SUBSTR(xb,i,1))+1)+1
next
ENDSCAN
SCAN
s=0
FOR i=1 TO 8
s=s+ctxt(i,VAL(SUBSTR(xb,i,1))+1)
next
IF s=8 &&满足条件,各个位置唯一
?xb
ENDIF
endscan
retu

weixin_41315215 2023-08-04
  • 打赏
  • 举报
回复

IF FILE(FILE)也错了,应该是IF FILE(FILE1)

weixin_41315215 2023-07-19
  • 打赏
  • 举报
回复

for k1=1 to s1之前漏了V0=0(初始值)

2,745

社区成员

发帖
与我相关
我的任务
社区描述
VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧