Submission #32786655


Source Code Expand

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
#define MOD 1000000007
#define rep(i,a,n) for (ll i=a;i<(ll)n;i++)
#define per(i,a,n) for (ll i=n;i-->(ll)a;)
#define pb push_back

ll read(){ll r;scanf("%lld",&r);return r;} // read

class KMP {
  private :
    vector<int> f; // 比它小的最长前缀的长度
    char *s;
    int sz;
  public:
    KMP(char *s,int sz):s(s),sz(sz){
      f = vector<int>(sz,0);
      int fa = 0;
      rep(i,1,sz){
        while (fa && s[i] != s[fa]) fa = f[fa-1];
        if (s[i] == s[fa]) fa++;
        f[i] = fa;
      }
    }
    vector<int> getPrefixLen(){
      return f;
    }
    int posIn(char *t,int szt) {
      int fa = 0;
      rep(i,0,szt) {
        while (fa && t[i] != s[fa]) fa = f[fa-1];
        if (t[i] == s[fa]) fa++;
        if (fa == sz) return i-fa+1;
      }
      return -1;
    }
};

char s[1000010];

int ns;
int nt;
const int INF = 0x3f3f3f3f;

int main(){
  scanf("%s",s);
  int ns = strlen(s);
  s[ns] = '#';
  scanf("%s",s+ns+1);
  int nt = strlen(s+ns+1);
  int n = ns+1+nt;
  vector<int> dp(nt+1,INF);
  dp[0] = 0;
  KMP kmp(s, n);
  auto pl = kmp.getPrefixLen();
  // rep(i,0,nt){
  //   printf("%lld: %d\n",i,pl[i+ns+1]);
  // }

  rep(i,1,nt+1){
    dp[i] = dp[i-pl[i+ns]]+1;
    // printf("dp[%lld] = %d\n",i,dp[i]);
  }
  printf("%d\n", dp[nt] >= INF?-1:dp[nt]);

  return 0;
}

// k 个S前缀拼成 T
// KMP?

Submission Info

Submission Time
Task G - Prefix Concatenation
User cromarmot
Language C++ (GCC 9.2.1)
Score 600
Code Size 1408 Byte
Status AC
Exec Time 22 ms
Memory 13956 KiB

Compile Error

./Main.cpp: In function ‘ll read()’:
./Main.cpp:10:21: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   10 | ll read(){ll r;scanf("%lld",&r);return r;} // read
      |                ~~~~~^~~~~~~~~~~
./Main.cpp: In function ‘int main()’:
./Main.cpp:48:8: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   48 |   scanf("%s",s);
      |   ~~~~~^~~~~~~~
./Main.cpp:51:8: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
   51 |   scanf("%s",s+ns+1);
      |   ~~~~~^~~~~~~~~~~~~

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 2
AC × 33
Set Name Test Cases
Sample example_00.txt, example_01.txt
All example_00.txt, example_01.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt
Case Name Status Exec Time Memory
example_00.txt AC 13 ms 3564 KiB
example_01.txt AC 2 ms 3496 KiB
hand_00.txt AC 14 ms 9300 KiB
hand_01.txt AC 10 ms 7608 KiB
hand_02.txt AC 16 ms 13788 KiB
hand_03.txt AC 15 ms 7512 KiB
hand_04.txt AC 11 ms 7492 KiB
hand_05.txt AC 11 ms 7616 KiB
random_00.txt AC 22 ms 13776 KiB
random_01.txt AC 16 ms 13828 KiB
random_02.txt AC 17 ms 13928 KiB
random_03.txt AC 13 ms 9368 KiB
random_04.txt AC 14 ms 9356 KiB
random_05.txt AC 18 ms 13508 KiB
random_06.txt AC 16 ms 13316 KiB
random_07.txt AC 16 ms 13548 KiB
random_08.txt AC 16 ms 13472 KiB
random_09.txt AC 18 ms 13776 KiB
random_10.txt AC 19 ms 13592 KiB
random_11.txt AC 16 ms 13824 KiB
random_12.txt AC 17 ms 13404 KiB
random_13.txt AC 19 ms 13956 KiB
random_14.txt AC 16 ms 13356 KiB
random_15.txt AC 21 ms 13884 KiB
random_16.txt AC 16 ms 13828 KiB
random_17.txt AC 16 ms 13700 KiB
random_18.txt AC 16 ms 13248 KiB
random_19.txt AC 14 ms 13204 KiB
random_20.txt AC 15 ms 13424 KiB
random_21.txt AC 16 ms 13312 KiB
random_22.txt AC 15 ms 13808 KiB
random_23.txt AC 16 ms 13880 KiB
random_24.txt AC 15 ms 13332 KiB