偶写了一个,MATLAB的,例程如下:
clear
clc
%status是一单元数组,一行代表一种状态,第一个元素存储对应的字母,第二个元素代表对应长度,第三个元素为在ch_in中对应的序号,第四个元素为对应的字符串
%2004.1.11
ch_in=input('Please input the objective string:','s');
ch_len=size(ch_in,2);
size_sta=0;
if ch_len==1
disp('Only one char???Please Input again!')
else
ch_in_decrea=ch_in(ch_len:-1:1);
ch_in=ch_in_decrea;
for i=1:ch_len
if i==1
size_sta=1;
status(1,={ch_in(1),1,1,ch_in(1)};
else
temp_numth=ones(1,size(status,1));
for k=1:size(status,1)
temp_numth(k)=status{k,1};
end
max_numtest2=find(temp_numth<=ch_in(i));
if isempty(max_numtest2)
size_sta=size_sta+1;
status(size_sta,={ch_in(i),1,i,ch_in(i)};
else
max_numtest=find(temp_numth<ch_in(i));
if ~isempty(max_numtest)
max_lentest=status{max_numtest(1),2};
max_numthtest=max_numtest(1);
for q=2:size(max_numtest,2)
if max_lentest<status{max_numtest(q),2}
max_lentest=status{max_numtest(q),2};
max_numthtest=max_numtest(q);
end
end
temp_len=status{max_numthtest,2}+1;
temp_equ=find([status{find([status{:,2}]==temp_len),1}]==ch_in(i));
if isempty(temp_equ)
temp_char=strcat(status{max_numthtest,4},ch_in(i));
size_sta=size_sta+1;
status(size_sta,={ch_in(i),temp_len,i,temp_char};
min_i=char(min(temp_numth(find(temp_numth>ch_in(i)))));
if ~isempty(min_i)
min_num=find(temp_numth==min_i);
for t=1:size(min_num,2)
if status{min_num(t),2}==temp_len
status(min_num(t),:)=[];
size_sta=size_sta-1;
end
end
end
end
end
end
end
end
sta_len=zeros(1,size_sta);
for q=1:size_sta
sta_len(q)=status{q,2};
end
[max_len,max_lennum]=max(sta_len);
disp('The char you need is: ')
disp_max=status{max_lennum,4}(max_len:-1:1);
disp(disp_max)
end |