Submission #1029836

#TimeUsernameProblemLanguageResultExecution timeMemory
1029836earlyamazonSavez (COCI15_savez)C++14
120 / 120
63 ms40284 KiB
// wzorcowka KMP i trie

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

const int mn = 1e6+7;
int n, terwyn, wyn, wsk = 2;
vector<int> ps;

struct node{
    vector<int> s;
    int dp = 0;
};

node trie[mn];

void zejdz(string s, int ind=1, int poz=0, int indps=0){
    // cout<<"a: "<<s<<" "<<indps<<" "<<poz<<" "<<ind<<"\n";
    for (int poz = 0; poz <= s.size(); poz++){
        if (indps < ps.size() && ps[indps] == poz) {
            terwyn = max(terwyn, trie[ind].dp);
            indps++;
        }
        if (poz == s.size()) break;
        if (trie[ind].s.size() < 26) trie[ind].s.resize(26);
        if (!trie[ind].s[s[poz]-'A']){
            trie[ind].s[s[poz]-'A'] = wsk++;
        }
        ind = trie[ind].s[s[poz] - 'A'];
    }
    trie[ind].dp = terwyn + 1;
    wyn = max(wyn, trie[ind].dp);
}

int main(){
    ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin>>n;
    string s;
    trie[1].s.resize(26);
    for (int i = 0; i < n; i++){
        cin>>s;
        vector<int> pi = {0};
        ps.clear();
        ps.push_back(s.size());
        terwyn = 0;
        for (int j = 1; j < s.size(); j++){
            int k = pi.back();
            while (s[j] != s[k] && k){
                k = pi[k-1];
            }
            pi.push_back(k);
            if (s[j] == s[k]) {
                pi.back()++;
            }
        }
        // for (auto j : pi) cout<<j<<" ";
        // cout<<"\n";
        int k = pi.back();
        while (k){
            ps.push_back(k);
            k = pi[k-1];
        }
        reverse(ps.begin(), ps.end());
        // for (auto j : ps) cout<<j<<" ";
        // cout<<"\n";
        zejdz(s);
    }
    cout<<wyn<<"\n";
}

Compilation message (stderr)

savez.cpp: In function 'void zejdz(std::string, int, int, int)':
savez.cpp:19:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   19 |     for (int poz = 0; poz <= s.size(); poz++){
      |                       ~~~~^~~~~~~~~~~
savez.cpp:20:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   20 |         if (indps < ps.size() && ps[indps] == poz) {
      |             ~~~~~~^~~~~~~~~~~
savez.cpp:24:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   24 |         if (poz == s.size()) break;
      |             ~~~~^~~~~~~~~~~
savez.cpp: In function 'int main()':
savez.cpp:46:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   46 |         for (int j = 1; j < s.size(); j++){
      |                         ~~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...