dongyidao1461 2014-07-19 19:43
浏览 29
已采纳

获取相同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
  • 写回答

2条回答 默认 最新

  • doyhq66282 2014-07-19 22:34
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助