Advertisement
cd62131

compare string

Oct 24th, 2018
516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 0.54 KB | None | 0 0
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use utf8;
  5. use open qw(:std :encoding(utf8));
  6. use feature q(say);
  7.  
  8. sub Compare($$);
  9. sub Rate($$);
  10.  
  11. my $s1 = 'あいう1';
  12. my $s2 = 'あいえ23';
  13. say Rate($s1, $s2) . '%';
  14.  
  15. sub Compare($$) {
  16.   my ($s1, $s2) = @_;
  17.   my $len1 = length $s1;
  18.   for (my $i = $len1; $i > 0; --$i) {
  19.     for (my $j = 0; $i + $j <= $len1; ++$j) {
  20.       return $i if $s2 =~ substr $s1, $j, $i;
  21.     }
  22.   }
  23.   return 0;
  24. }
  25. sub Rate($$) {
  26.   my ($s1, $s2) = @_;
  27.   return Compare($s1, $s2) * 100.0 / (length $s2);
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement