#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=int;
const ll maxN=5e5+10;
const ll inf=1e18;
const ll mod=1e9+7;
struct TrieNode
{
ll val;
TrieNode* child[26];
TrieNode()
{
val=0;
for(int i=0;i<26;i++) child[i]=nullptr;
}
};
TrieNode *root=new TrieNode();
void add(string s,ll val)
{
auto p=root;
ll n=s.size()-1;
for(int i=1;i<n;i++)
{
if(p->child[s[i]-'a']==nullptr)
{
p->child[s[i]-'a']=new TrieNode();
}
p=p->child[s[i]-'a'];
}
p->val=max(p->val,val);
for(int i=n;i<=n;i++)
{
if(p->child[s[i]-'a']==nullptr)
{
p->child[s[i]-'a']=new TrieNode();
}
p=p->child[s[i]-'a'];
}
p->val=max(p->val,val);
}
ll get(string s)
{
auto p=root;
ll ans=0;
ll n=s.size()-1;
for(int i=1;i<n;i++)
{
if(p->child[s[i]-'a']==nullptr)
{
p->child[s[i]-'a']=new TrieNode();
}
p=p->child[s[i]-'a'];
}
ans=max(ans,p->val);
for(int i=n;i<=n;i++)
{
if(p->child[s[i]-'a']==nullptr)
{
p->child[s[i]-'a']=new TrieNode();
}
p=p->child[s[i]-'a'];
}
ans=max(ans,p->val);
return ans;
}
ll n;
string s[maxN];
void solve()
{
cin >> n;
ll ans=0;
for(int i=1;i<=n;i++)
{
cin >> s[i];
reverse(s[i].begin(),s[i].end());
s[i]=' '+s[i];
ll x=get(s[i])+1;
ans=max(ans,x);
add(s[i],x);
}
cout << ans;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
rima.cpp:9:14: warning: overflow in conversion from 'double' to 'll' {aka 'int'} changes value from '1.0e+18' to '2147483647' [-Woverflow]
9 | const ll inf=1e18;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
10 ms |
15956 KB |
Output isn't correct |
2 |
Correct |
9 ms |
15956 KB |
Output is correct |
3 |
Incorrect |
8 ms |
15980 KB |
Output isn't correct |
4 |
Incorrect |
212 ms |
151828 KB |
Output isn't correct |
5 |
Incorrect |
30 ms |
20684 KB |
Output isn't correct |
6 |
Incorrect |
63 ms |
83632 KB |
Output isn't correct |
7 |
Incorrect |
62 ms |
83172 KB |
Output isn't correct |
8 |
Incorrect |
69 ms |
82944 KB |
Output isn't correct |
9 |
Incorrect |
88 ms |
89728 KB |
Output isn't correct |
10 |
Incorrect |
60 ms |
83112 KB |
Output isn't correct |