% DnaPatternMatching.m
% Finding the best match (with the fewest number of errors) between a
% given string pattern and a longer string.
% The starting location in the longer string is found.
% The number of errors with the fewest errors is reported.
% K. Ming Leung, Nov. 12, 2008.

clear all;

Dna = readStringFromTextFile('sd4.dna');

Dozer = readStringFromTextFile('Dozer.dna');

n = length(Dna), m = length(Dozer),

for i = 1:(n-m+1)
    NumErrors(i) = sum( Dna(i:i+m-1) ~= Dozer );
end

[minError, bestMatchPosition] = min(NumErrors),

disp(['A best match with ' num2str(minError) ' errors was found starting at position ' num2str(bestMatchPosition)]);
chance = max([100-5*minError, 0]);

disp(['There is a ' num2str(chance) '% chance that this student is a dozer.']);


% sd1: minError = 1, bestMatch = 5046   n = 10010, m = 128
% sd2: minError = 0, bestMatch = 3011   n = 11001, m = 128
% sd3: minError = 0, bestMatch = 6343   n = 10010, m = 128
% sd4: minError = 3, bestMatch = 990    n = 10010, m = 128

% Idx = zeros(maxError-minError+1,n-m+1);
% 
% i = 1;
% for nerrors = minError:maxError
%     idx = find(NumErrors == nerrors),
%     Idx(i,1:length(idx)) = idx,
%     Ntimes(i) = length(idx);
%     Nerrors(i) = nerrors;
%     i = i+1;
% end
% 
% % for nerrors1 = 1:m+1
% %      idx = find( NumErrors == nerrors1-1),
% %      Idx(nerrors1,1:length(idx)) = idx,
% %     Ntimes(nerrors1) = length(idx);
% %     Nerrors(nerrors1) = nerrors1 - 1;
% % end
% 
