답안 #475343

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
475343 2021-09-22T03:44:39 Z cpp219 Rima (COCI17_rima) C++14
56 / 140
691 ms 106320 KB
#include<bits/stdc++.h>
#define ll long long
#define ld long double
#define fs first
#define sc second
#define debug(y) cout<<y,exit(0)
using namespace std;
typedef pair<ll,ll> LL;
typedef pair<ld,ll> LD;
const ll N = 5e5 + 9;
const ll mod = 1e9 + 7;
const ll base = 31;

string s[N];
vector<ll> Hash[N];
ll n,PW[N*6],ans;
unordered_map<ll,ll> val;

bool lf(string a,string b){
    return (a.size() < b.size() || a.size() == b.size() && a < b);
}

ll GetHash(ll id,ll L,ll R){
    return (Hash[id][R] - Hash[id][L - 1]*PW[R - L + 1] + mod*mod)%mod;
}

ll Prefix1(ll id){
    return GetHash(id,1,s[id].size() - 2);
}

ll PrefixAll(ll id){
    return Hash[id][s[id].size() - 1];
}

int main(){
    ios_base::sync_with_stdio(NULL); cin.tie(0); cout.tie(0);
    #define task "tst"
    if (fopen(task".inp","r")){
        freopen(task".inp","r",stdin);
        //freopen(task".out","w",stdout);
    }
    PW[0] = 1;
    for (ll i = 1;i < N*6;i++) PW[i] = (PW[i - 1] * base)%mod;
    cin>>n;
    for (ll i = 1;i <= n;i++){
        cin>>s[i]; reverse(s[i].begin(),s[i].end());
        s[i] = ' ' + s[i];
    }
    sort(s + 1,s + n + 1,lf);
    for (ll i = 1;i <= n;i++){
        ll sz = s[i].size() - 1; Hash[i].resize(sz + 1);
        for (ll j = 1;j <= sz;j++)
            Hash[i][j] = (Hash[i][j - 1] * base + s[i][j] - 'a' + 1)%mod;
    }

    //for (ll i = 1;i <= n;i++) cout<<s[i]<<"\n"; exit(0);
    //cout<<Prefix1(4)<<" "<<GetHash(4,1,3); return 0;
    for (ll i = 1;i <= n;i){
        //cout<<i<<"\n";
        ll j = i + 1,prv = Prefix1(i);
        while(j <= n && prv == Prefix1(j) && s[j].size() == s[i].size()) j++;
        for (ll l = i;l < j;l++){
            ll now = PrefixAll(l);
            val[now] = max(val[now],j - i + val[prv]);
            ans = max(ans,val[now]);
        }
        //cout<<i<<" "<<j<<"\n";
        i = j; //debug(i);
    }
    cout<<ans;
}

/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/

Compilation message

rima.cpp: In function 'bool lf(std::string, std::string)':
rima.cpp:20:57: warning: suggest parentheses around '&&' within '||' [-Wparentheses]
   20 |     return (a.size() < b.size() || a.size() == b.size() && a < b);
      |                                    ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~
rima.cpp: In function 'int main()':
rima.cpp:58:26: warning: for increment expression has no effect [-Wunused-value]
   58 |     for (ll i = 1;i <= n;i){
      |                          ^
rima.cpp:39:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
   39 |         freopen(task".inp","r",stdin);
      |         ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 45 ms 51136 KB Output is correct
2 Correct 39 ms 51160 KB Output is correct
3 Correct 40 ms 51068 KB Output is correct
4 Incorrect 691 ms 106320 KB Output isn't correct
5 Correct 90 ms 78624 KB Output is correct
6 Incorrect 55 ms 56816 KB Output isn't correct
7 Incorrect 49 ms 55740 KB Output isn't correct
8 Incorrect 47 ms 55052 KB Output isn't correct
9 Incorrect 117 ms 81424 KB Output isn't correct
10 Incorrect 50 ms 55380 KB Output isn't correct