제출 #367372

#제출 시각아이디문제언어결과실행 시간메모리
367372GusMGNecklace (Subtask 4) (BOI19_necklace4)C++17
0 / 15
129 ms65536 KiB
#include <bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=(a);i<(b);i++) #define per(i,a,b) for(int i=(b)-1;i>=(a);i--) using ll=long long; using vi=vector<int>; //pi[i] = longitud del prefijo propio mas grande de s[0..i] que tambien es sufijo vi prefix_function(string s) { vi pi(s.size()); for(int i=1,j=0; i<s.size(); i++) { while(j && s[i]!=s[j]) j=pi[j-1]; if(s[i]==s[j]) j++; pi[i] = j; } return pi; } // Busca las coincidencias de p en s, regresa indices de inicio vi kmp(string s, string p) { vi pi = prefix_function(p); vi ans(s.size()); for(int i=0,j=0; i<s.size(); i++) { while(j && s[i]!=p[j]) j=pi[j-1]; if(s[i]==p[j]) j++; ans[i]=j; if(j==p.size()) { j=pi[j-1]; } } return ans; } int main(){ ios_base::sync_with_stdio(0); string s,t; cin>>s>>t; int n=s.size(),m=t.size(),ans=0,l=0,r=0; s=s+"$"+s; n=2*n+1; reverse(s.begin()+n/2+1,s.end()); vector<vi> lps(m),lsp(n); rep(k,0,m) { string p=t.substr(k); lps[k]=kmp(s,p); } rep(k,0,n) { string p=s.substr(k); lsp[k]=kmp(t,p); } rep(i,0,n-1) rep(j,0,m-1) { int a=lps[j+1][i],b=lsp[i+1][j]; if(ans<a+b) ans=a+b,l=i<n/2?i-a+1:n-i-b-1,r=j-b+1; } cout<<ans<<"\n"; cout<<l<<" "<<r<<"\n"; }

컴파일 시 표준 에러 (stderr) 메시지

necklace.cpp: In function 'vi prefix_function(std::string)':
necklace.cpp:10:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   10 |     for(int i=1,j=0; i<s.size(); i++) {
      |                      ~^~~~~~~~~
necklace.cpp: In function 'vi kmp(std::string, std::string)':
necklace.cpp:21:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     for(int i=0,j=0; i<s.size(); i++) {
      |                      ~^~~~~~~~~
necklace.cpp:25:13: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   25 |         if(j==p.size()) {
      |            ~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...