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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀