結果

問題 No.430 文字列検索
ユーザー HachimoriHachimori
提出日時 2016-10-02 23:39:46
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 125 ms / 2,000 ms
コード長 1,597 bytes
コンパイル時間 610 ms
コンパイル使用メモリ 69,632 KB
実行使用メモリ 4,768 KB
最終ジャッジ日時 2023-08-15 17:26:57
合計ジャッジ時間 2,332 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,384 KB
testcase_01 AC 79 ms
4,748 KB
testcase_02 AC 125 ms
4,564 KB
testcase_03 AC 47 ms
4,464 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 34 ms
4,768 KB
testcase_09 AC 1 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 80 ms
4,404 KB
testcase_12 AC 80 ms
4,428 KB
testcase_13 AC 80 ms
4,616 KB
testcase_14 AC 90 ms
4,564 KB
testcase_15 AC 90 ms
4,404 KB
testcase_16 AC 80 ms
4,480 KB
testcase_17 AC 87 ms
4,476 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
const int BUF = 50005;
const int WORD = 5005;


class Sorter{
public:
  
  int *v, h;
  
  Sorter(){}
  Sorter(int *v, int h): v(v), h(h){}

  bool operator()(int a, int b){
    if(a==b) return false;
    if(v[a]!=v[b]) return v[a]<v[b];
    return v[a+h]<v[b+h];
  }
};

vector<char*> construct(char *txt){
  int N = strlen(txt);
  static int V[BUF], pos[BUF], buf[BUF];
  txt[N++] = '\0';
  
  for(int i=0;i<N;i++){ 
    pos[i] = i; 
    V[i] = txt[i]; 
  }
  sort(pos,pos+N,Sorter(V,0));
  
  for(int h=1;h<N;h<<=1){
    Sorter sorter(V,h);
    sort(pos,pos+N,sorter);
    
    buf[0] = 0;
    for(int i=1;i<N;i++)
      buf[i] = buf[i-1] + !(V[pos[i-1]]==V[pos[i]] && V[pos[i-1]+h]==V[pos[i]+h]);
    
    for(int i=0;i<N;i++)
      V[pos[i]] = buf[i];
  }

  vector<char*> ret;
  for(int i=0;i<N;i++)
    ret.push_back(txt+pos[i]);
  
  return ret;
}


char txt[BUF];
int nWord;
string word[WORD];
    
void read() {
    cin >> txt;
    cin >> nWord;
    for (int i = 0; i < nWord; ++i) {
        cin >> word[i];
    }
}


void work() {
    vector<char*> sarray = construct(txt);

    int cnt = 0;
    for (int i = 0; i < nWord; ++i) {
        vector<char*>::iterator bgn = lower_bound(sarray.begin(), sarray.end(), word[i]);
        
        ++word[i][word[i].size() - 1];
    
        vector<char*>::iterator end = lower_bound(sarray.begin(), sarray.end(), word[i]);
        
        cnt += end - bgn;
    }
    
    cout << cnt << endl;
}


int main() {
    read();
    work();
    return 0;
}
0