获取相同PHP代码的不同输出

(Can't paste the exact question as the contest is over and I am unable to access the question. Sorry.)

Hello, recently I took part in a programming contest (PHP). I tested the code on my PC and got the desired output but when I checked my code on the contest website and ideone, I got wrong output. This is the 2nd time the same thing has happened. Same PHP code but different output.

It is taking input from command line. The purpose is to bring substrings that contact the characters 'A','B','C','a','b','c'.

For example: Consider the string 'AaBbCc' as CLI input. Substrings: A,a,B,b,C,c,Aa,AaB,AaBb,AaBbC,AaBbCc,aB,aBb,aBbC,aBbCc,Bb,BbC,BbCc,bC,bCc,Cc.

Total substrings: 21 which is the correct output.

My machine:

Windows 7 64 Bit PHP 5.3.13 (Wamp Server)

Following is the code:

<?php
$stdin = fopen('php://stdin', 'r');
    while(true) {
        $t = fread($stdin,3);
        $t = trim($t);
        $t = (int)$t;
        while($t--) {
            $sLen=0;
            $subStringsNum=0;
            $searchString="";
            $searchString = fread($stdin,20);
            $sLen=strlen($searchString);
            $sLen=strlen(trim($searchString));
            for($i=0;$i<$sLen;$i++) {
                for($j=$i;$j<$sLen;$j++) {                  
                    if(preg_match("/^[A-C]+$/i",substr($searchString,$i,$sLen-$j))) {$subStringsNum++;}
                }
            }
            echo $subStringsNum."
";

        }
        die;
    }
?>

Input:
2
AaBbCc
XxYyZz

Correct Output (My PC):
21
0

Ideone/Contest Website Output:
20
0

You have to keep in mind that your code is also processing the newline symbols.

On Windows systems, newline is composed by two characters, which escaped representation is .

On UNIX systems including Linux, only is used, and on MAC they use instead.

Since you are relying on the standard output, it will be susceptible to those architecture differences, and even if it was a file you are enforcing the architecture standard by using the flag "r" when creating the file handle instead of "rb", explicitly declaring you don't want to read the file in binary safe mode.

You can see in in this Ideone.com version of your code how the PHP script there will give the expected output when you enforce the newline symbols used by your home system, while in this other version using UNIX newlines it gives the "wrong" output.

I suppose you should be using fgets() to read each string separetely instead of fread() and then trim() them to remove those characters before processing.

I tried to analyse this code and that's what I know:

  1. It seems there are no problems with input strings. If there were any it would be impossible to return result 20

  2. I don't see any problem with loops, I usually use pre-incrementation but it shouldn't affect result at all

There are only 2 possibilities for me that cause unexpected result:

  1. One of the loops iteration isn't executed - it could be only the last one inner loop (when $i == 5 and then $j == 5 because this loop is run just once) so it will match difference between 21 and 20.

  2. preg_match won't match this string in one of occurrences (there are 21 checks of preg_match and one of them - possible the last one doesn't match).

If I had to choose I would go for the 1st possible cause. If I were you I would contact concepts author and ask them about version and possibility to test other codes. In this case the most important is how many times preg_match() is launched at all - 20 or 21 (using simple echo or extra counter would tell us that) and what are the strings that preg_match() checks. Only this way you can find out why this code doesn't work in my opinion.

It would be nice if you could put here any info when you find out something more.

PS. Of course I also get result 21 so it's hard to say what could be wrong