1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use warnings;
use strict;
my @data = qw(
# ...
);
my @new;
my @data_tmp = @data;
while (1) {
my %test;
@new = ();
for my $item (@data_tmp) {
next if $item !~ /^\d{2,}$/;
my $suffix = chop($item);
$test{$item}{$suffix}++;
}
for my $base (keys %test) {
if (scalar(keys %{$test{$base}}) == 10) {
push @new, $base;
}
else {
push @new, $base.$_ for keys %{$test{$base}};
}
}
last if $#new == $#data_tmp;
@data_tmp = @new;
}
print "$_\n" for sort @new;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$data = array(
'123456',
'123457'
);
$new = array();
$data_tmp = $new;
while (1) {
$test = array();
$new = array();
foreach ($data_tmp as $item) {
if (preg_match('/^\d{2,}/',$item) ) {
$suffix = substr($item,0,length($item)-1);
$test[$item][$suffix]+=1;
}
}
foreach (array_keys($test) as $base) {
if (count(array_keys($test[$base])) == 10) {
array_push($new, $base);
}
else {
foreach (array_keys($test[$base]) as $k) {
array_push($new, $base . $k);
}
}
if (count($new) == count($data_tmp)) {
goto leavewhile;
}
$data_tmp = $new;
}
leavewhile:
foreach (sort($new) as $val) {
echo $val . "\n";
}