답안 #994107

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
994107 2024-06-07T06:32:38 Z vjudge1 Rima (COCI17_rima) C++17
56 / 140
519 ms 78740 KB
#include <bits/stdc++.h>

using namespace std;

#define int long long

const long long mod = 1LL<<42;
const int M = 3e6, N = M/20;

long long pw[M];
bool not_pr[N];

signed main()
{
	ios::sync_with_stdio(0);
	cin.tie(NULL),cout.tie(NULL);
	vector<long long> hashes;
	for (int i=2;i<N;i++)
		for (int j=i+i;j<N;j+=i)
			not_pr[j]=true;
	for (int i=100;i<N;i++)
		if (!not_pr[i])
		{
			hashes.push_back(i);
			i+=100;
		}
	srand(time(NULL));
	srand(rand());
	long long h=hashes[rand()%1372];
	
	pw[0]=1;
	for (int i=1;i<M;i++)
		pw[i]=pw[i-1]*h%mod;
	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());
	unordered_map<long long,int> ans;
	ans.reserve(500369);
	while (!v.empty())
	{
		string s=v.back();
		v.pop_back();
		long long hs=0,m=s.size();
		for (int i=1;i<m;i++)
			hs=hs*h+s[i],hs%=mod;
		int mx;
		if (ans.find(hs)!=ans.end())
			mx=ans[hs]+1;
		else
			mx=1;
		vector<long long> wht;
		long long p;
		for (int c='a';c<='z';c++)
		{
			p=hs;
			p+=pw[m-1]*c%mod;
			p%=mod;
			if (ans.find(p)!=ans.end())
				mx=max(mx,ans[p]+1),wht.push_back(p);
			else if(c==s[0])
				wht.push_back(p);
				
		}
		for (long long i:wht)
			ans[i]=mx;
	}
	int res=0;
	for (auto i:ans)
		res=max(res,i.second);
	cout<<res<<endl;
	
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 22 ms 27996 KB Output is correct
2 Correct 18 ms 27996 KB Output is correct
3 Correct 17 ms 28192 KB Output is correct
4 Incorrect 519 ms 78740 KB Output isn't correct
5 Correct 32 ms 34384 KB Output is correct
6 Incorrect 23 ms 30228 KB Output isn't correct
7 Incorrect 24 ms 29784 KB Output isn't correct
8 Incorrect 21 ms 29584 KB Output isn't correct
9 Incorrect 42 ms 36240 KB Output isn't correct
10 Incorrect 21 ms 29604 KB Output isn't correct