#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#define TEXT_SIZE 1000000
#define KEY "mouri"
#define KEY_SIZE (sizeof(KEY) - 1)
// 乱数によるアルファベット文字列生成
void generate_random_text(char *text, int size) {
const char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < size; i++) {
text
[i
] = alphabet
[rand() % (sizeof(alphabet
) - 1)]; }
text[size] = '\0';
}
// 文字列を探索する関数
char* search(char *text, const char *key) {
for (char *p = text; p <= text + m - n; p++) {
return p;
}
}
return NULL;
}
int main() {
// 乱数のシードを設定
// 乱数による1000000文字のアルファベット文字列を生成
char text[TEXT_SIZE + 1];
generate_random_text(text, TEXT_SIZE);
// キーを探索
char *p = search(text, KEY);
while (p != NULL) {
printf("keyは%d文字目から%d文字目までで、それ以降の文字列は%sとなります\n", p - text + 1, p - text + 1 + KEY_SIZE, p);
p = search(p + KEY_SIZE, KEY);
}
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KI2luY2x1ZGUgPHN0cmluZy5oPgojaW5jbHVkZSA8dGltZS5oPgoKI2RlZmluZSBURVhUX1NJWkUgMTAwMDAwMAojZGVmaW5lIEtFWSAibW91cmkiCiNkZWZpbmUgS0VZX1NJWkUgKHNpemVvZihLRVkpIC0gMSkKCi8vIOS5seaVsOOBq+OCiOOCi+OCouODq+ODleOCoeODmeODg+ODiOaWh+Wtl+WIl+eUn+aIkAp2b2lkIGdlbmVyYXRlX3JhbmRvbV90ZXh0KGNoYXIgKnRleHQsIGludCBzaXplKSB7CiAgICBjb25zdCBjaGFyIGFscGhhYmV0W10gPSAiYWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4eXoiOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBzaXplOyBpKyspIHsKICAgICAgICB0ZXh0W2ldID0gYWxwaGFiZXRbcmFuZCgpICUgKHNpemVvZihhbHBoYWJldCkgLSAxKV07CiAgICB9CiAgICB0ZXh0W3NpemVdID0gJ1wwJzsKfQoKLy8g5paH5a2X5YiX44KS5o6i57Si44GZ44KL6Zai5pWwCmNoYXIqIHNlYXJjaChjaGFyICp0ZXh0LCBjb25zdCBjaGFyICprZXkpIHsKICAgIGludCBtID0gc3RybGVuKHRleHQpOwogICAgaW50IG4gPSBzdHJsZW4oa2V5KTsKICAgIGZvciAoY2hhciAqcCA9IHRleHQ7IHAgPD0gdGV4dCArIG0gLSBuOyBwKyspIHsKICAgICAgICBpZiAoc3RybmNtcChwLCBrZXksIG4pID09IDApIHsKICAgICAgICAgICAgcmV0dXJuIHA7CiAgICAgICAgfQogICAgfQogICAgcmV0dXJuIE5VTEw7Cn0KCmludCBtYWluKCkgewogICAgLy8g5Lmx5pWw44Gu44K344O844OJ44KS6Kit5a6aCiAgICBzcmFuZCh0aW1lKE5VTEwpKTsKCiAgICAvLyDkubHmlbDjgavjgojjgosxMDAwMDAw5paH5a2X44Gu44Ki44Or44OV44Kh44OZ44OD44OI5paH5a2X5YiX44KS55Sf5oiQCiAgICBjaGFyIHRleHRbVEVYVF9TSVpFICsgMV07CiAgICBnZW5lcmF0ZV9yYW5kb21fdGV4dCh0ZXh0LCBURVhUX1NJWkUpOwoKICAgIC8vIOOCreODvOOCkuaOoue0ogogICAgY2hhciAqcCA9IHNlYXJjaCh0ZXh0LCBLRVkpOwogICAgd2hpbGUgKHAgIT0gTlVMTCkgewogICAgICAgIHByaW50Zigia2V544GvJWTmloflrZfnm67jgYvjgoklZOaWh+Wtl+ebruOBvuOBp+OBp+OAgeOBneOCjOS7pemZjeOBruaWh+Wtl+WIl+OBryVz44Go44Gq44KK44G+44GZXG4iLAogICAgICAgICAgICAgICBwIC0gdGV4dCArIDEsIHAgLSB0ZXh0ICsgMSArIEtFWV9TSVpFLCBwKTsKICAgICAgICBwID0gc2VhcmNoKHAgKyBLRVlfU0laRSwgS0VZKTsKICAgIH0KCiAgICByZXR1cm4gMDsKfQ==