# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
994060 | vjudge1 | Rima (COCI17_rima) | C++17 | 1101 ms | 56492 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int h = 727, mod = 1e9 + 7;
const int h1 = 7369, mod1 = 998244353;
const int enc = 8369, mod2 = 1LL<<36;
int power(int a,int b,int md)
{
int ans=1;
while (b)
{
if (b&1)
ans=ans*a%md;
a=a*a%md;
b>>=1;
}
return ans;
}
int en(int has,int has1)
{
return (has1*enc+has)%mod2;
}
signed main()
{
int n;
cin>>n;
map<int,vector<string>> mp;
for (int i=0;i<n;i++)
{
string s;
cin>>s;
mp[s.size()].push_back(s);
}
vector<string> v;
for (auto i:mp)
for (auto j:i.second)
v.push_back(j);
reverse(v.begin(),v.end());
map<int,int> ans;
while (!v.empty())
{
string s=v.back();
v.pop_back();
int hs=0,hs1=0,m=s.size();
for (int i=1;i<m;i++)
{
hs=hs*h+s[i],hs1=hs1*h1+s[i];
hs%=mod,hs1%=mod1;
}
int mx;
if (ans.find(en(hs,hs1))!=ans.end())
mx=ans[en(hs,hs1)]+1;
else
mx=1;
for (int c='a';c<='z';c++)
{
pair<int,int> p={hs,hs1};
p.first+=power(h,m-1,mod)*c%mod;
p.second+=power(h1,m-1,mod1)*c%mod1;
p.first%=mod,p.second%=mod1;
if (ans.find(en(p.first,p.second))!=ans.end())
mx=max(mx,ans[en(p.first,p.second)]+1);
}
for (int c='a';c<='z';c++)
{
pair<int,int> p={hs,hs1};
p.first+=power(h,m-1,mod)*c%mod;
p.second+=power(h1,m-1,mod1)*c%mod1;
p.first%=mod,p.second%=mod1;
if (ans.find(en(p.first,p.second))!=ans.end() or c==s[0])
ans[en(p.first,p.second)]=mx;
}
}
int res=0;
for (auto i:ans)
res=max(res,i.second);
cout<<res<<endl;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |