wangyongdong
9/19/2019 - 7:10 AM

要求我们把圣经(bbe.txt)背熟,直至他说哪个单词,我们就要飞快的回答出这个单词在第几行第几个单词位置

/**
 * 我们碰到了大麻烦,一个新来的传教士惹恼了上帝,上帝很愤怒,要求我们把圣经(bbe.txt)背熟,直至他说哪个单词,我们就要飞快的回答出这个单词在第几行第几个单词位置。听说你是个优秀的程序员,那么髟助我们完成这个不可能的任务吧。
  要求如下:
  1)/myworks/example/bbe.txt,98版本英文圣经一本
  2)输入部分要求如下:php ./example.php [单词]
  3)输出部分如下:[单词] 1,2  2,4  5,6 表示:此单词在1行2列(第二个单词),2行4列...
  说明:
  1)此文本4MB之巨...
  2)单词的含义:由英文字母(大小写),数字(0-9)组成的串
  3)提供给你的机器OS为ubuntu 9.10,内存只有1G,而且,很不幸的,其中700M用来做了别的
  4)上机考试不允许上网,但我装了man文档以及读取CHM以及PDF的阅读器,在电脑的桌面的CHM文件夹中,有相应的PHP参考手册
  5)算法复杂度要求不能大于O(N^2)(就是N的平方)
  6)什么?PHP低效且用起来不顺手,好的,你可以用别的语言来实现。但注意:提供给你的机器上只有python 2.4/perl 5.8/gcc[g++] 4.1
 */

/**
 * Class WordMatch
 *  匹配单词
 */
class WordMatch
{
    /**
     * @var 文件名
     */
    public $file;

    /**
     * @var int 行数
     */
    public $line = 0;

    /**
     * WordMatch constructor.
     * @param $filename
     */
    public function __construct($filename)
    {
        $this->file = $filename;
    }

    /**
     * @param $word
     */
    public function get($word) {
        $this->readText($word);
    }

    /**
     * 读取文件,判断每行是否有匹配词
     * @param $word
     */
    public function readText($word) {
        $handle = fopen($this->file, "r");
        if ($handle) {
            while (!feof($handle)) {
                $lines = fgets($handle, 4096);
                $lines = str_replace(array("\r\n", "\n", "\r", " \r\n", " \n", " \r"), '', $lines);
                $this->line++;
                if(!empty($lines)) {
                    $number = $this->match($lines, $word);
                    if(!empty($number)) {
                        echo $this->line . "," . $number . "\r\n";
                    }
                }
            }
            fclose($handle);
        }
    }

    /**
     * 从一行中匹配词
     * @param $lines
     * @param $word
     */
    public function match($lines, $word) {
        // 判断此行是否包含
        $place = strpos($lines, $word);
        if($place !== false) {
            return $this->position($lines, $word);
        }
    }

    /**
     * 查询字符在字符串中每一次出现的位置
     * @param string $str       规定被搜索的字符串
     * @param string $find      规定要查找的字符
     * @param int    $offset    规定开始搜索的位置
     * @param string $storage   返回结果
     * @return string
     */
    public function position($str, $find, $offset = 0, $storage = '') {
        if(empty($str) || empty($find) || $offset > strlen($str)) {
            return false;
        }
        $pos = mb_stripos($str, $find, $offset);
        // 由于此函数返回的是返回字符串在另一个字符串中第一次出现的位置。所以这个位置有可能是0
        if($pos === false) {
            return $storage;
        }
        $offset = $pos+1;
        $storage .= $offset . ',';
        // 递归查找
        $storage = $this->position($str, $find, $offset, $storage);
        //    echo substr_count($str, $find);
        return $storage;
    }
}
$match = new WordMatch("bbe.txt");
$match->get('Development');
In the beginning God created the heavens and the earth.
Now the earth was formless and empty, darkness was over the surface of the deep, and the Spirit of God was hovering over the waters.
And God said, "Let there be light," and there was light.
God saw that the light was good, and he separated the light from the darkness.
God called the light "day," and the darkness he called "night." And there was evening, and there was morning--the first day.
And God said, "Let there be an expanse between the waters to separate water from water."
So God made the expanse and separated the water under the expanse from the water above it. And it was so.
God called the expanse "sky." And there was evening, and there was morning--the second day.
And God said, "Let the water under the sky be gathered to one place, and let dry ground appear." And it was so.
God called the dry ground "land," and the gathered waters he called "seas." And God saw that it was good.
Then God said, "Let the land produce vegetation: seed-bearing plants and trees on the land that bear fruit with seed in it, according to their various kinds." And it was so.
Development The land produced vegetation: plants bearing seed according to their kinds and trees bearing fruit with seed in it according to their kinds. And God saw that it was good.
And there was evening, and there was morning--the third day.
And God said, "Let there be lights in the expanse of the sky to separate the day from the night, and let them serve as signs to mark seasons and days and years,
and let them be lights in the expanse of the sky to give light on the earth." And it was so.
God made two great lights--the greater light to govern the day and the lesser light to govern the night. He also made the stars.
God set them in the expanse of the sky to give light on the earth,
to govern the day and the night, and to separate light from darkness. And God saw that it was good.
And there was evening, and there was morning--the fourth day.
And God said, "Let the water teem with living creatures, and let birds fly above the earth across the expanse of the sky."
So God created the great creatures of the sea and every living and moving thing with which the water teems, according to their kinds, and every winged bird according to its kind. And God saw that it was good.
God blessed them and said, "Be fruitful and increase in number and fill the water in the seas, and let the birds increase on the earth."
And there was evening, and there was morning--the fifth day.
And God said, "Let the land produce living creatures according to their kinds: livestock, creatures that move along the ground, and wild animals, each according to its kind." And it was so.
God made the wild animals according to their kinds, the livestock according to their kinds, and all the creatures that move along the ground according to their kinds. And God saw that it was good.
Then God said, "Let us make man in our image, in our likeness, and let them rule over the fish of the sea and the birds of the air, over the livestock, over all the earth, and over all the creatures that move along the ground."
So God created man in his own image, in the image of God he created him; male and female he created them.
God blessed them and said to them, "Be fruitful and increase in number; fill the earth and subdue it. Rule over the fish of the sea and the birds of the air and over every living creature that moves on the ground."
Then God said, "I give you every seed-bearing plant on the face of the whole earth and every tree that has fruit with seed in it. They will be yours for food.
And to all the beasts of the earth and all the birds of the air and all the creatures that move on the ground--everything that has the breath of life in it--I give every green plant for food." And it was so.
God saw all that he had made, and it was very good. And there was evening, and there was morning--the sixth day.