示例#1
0
 *
 * PHP Version 5
 *
 * @category  PHP
 * @package   HackerRank
 * @author    Laith Shadeed <*****@*****.**>
 * @copyright 2015 Laith Shadeed
 * @license   Public Domain
 * @link      https://www.hackerrank.com/challenges/anagram
 */
/**
 * Test by running
 * $ diff -u <(cat anagram.input | php anagram.php) anagram.output
 */
$input = new SplFileObject("php://stdin");
$input->fscanf("%d");
while (list($str) = $input->fscanf("%s")) {
    $n = strlen($str);
    // No integer division in PHP.
    $a = $n / 2;
    // If $a is not int, then $n is odd.
    if (!is_int($a)) {
        print "-1\n";
        continue;
    }
    // Hash the second word.
    $h = [];
    for ($i = $a; $i < $n; $i++) {
        $c = $str[$i];
        $h[$c] = isset($h[$c]) ? $h[$c] + 1 : 1;
    }
示例#2
0
<?php

// test
$result = null;
$f = new SplFileObject(__FILE__, 'r');
var_dump($f->fscanf('<?php // %s', $result));
var_dump($result);
var_dump($f->fscanf('<?php // %s'));
<?php

$obj = new SplFileObject(dirname(__FILE__) . '/SplFileObject_testinput.csv');
var_dump($obj->fscanf('%s'));