#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define mod 1000000007
#define mxe(v) *max_element(v.begin(), v.end())
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(),v.rend()
#define F first
#define S second
#define pi pair<ll,ll>
#define derar ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define yes cout<<"YES"<<endl;
#define no cout<<"NO"<<endl;
using namespace std;
const int N=1e5+5;
const double PI=3.1415926535897932384626433;
const double eps = 1e-9;
string getString()
{
char c[(int)2e6];
//scanf(" %c",&a[i]);
scanf("%s", c);
return c;
//string x = getString();
}
///printf("%d\n",);
///scanf("%d",&);
///cout.precision(10);
///puts("")string
const int CHAR = 26;
int ma=0;
vector<char>an;
struct TrieNode
{
TrieNode *child[CHAR];
int Cnt, End,deep;
TrieNode()
{
memset(child, 0, sizeof child);
Cnt = End = 0,deep=0;
}
void add(int i, string &s,int se)
{
int cur = s[i] - 'a';
if (child[cur] == 0) child[cur] = new TrieNode();
child[cur]->Cnt++;
child[cur]->deep=max(se,child[cur]->deep);
if (i == s.size() - 1)
{
child[cur]->End++;
return;
}
child[cur]->add(i + 1, s,se);
}
void add(string &s,int se)
{
add(0, s,se);
}
void erase(int i, string &s)
{
int cur = s[i] - 'a';
child[cur]->Cnt--;
if (i == s.size() - 1)
{
child[cur]->End--;
return;
}
child[cur]->erase(i + 1, s);
if (!child[cur]->Cnt)
{
delete child[cur];
child[cur] = 0;
}
}
void erase(string &s)
{
if (!find(s))return;
erase(0, s);
}
bool find(int i, string &s)
{
int cur = s[i] - 'a';
if (child[cur] == 0)return 0;
if (i == s.size() - 1)
{
return child[cur]->End > 0;
}
return child[cur]->find(i + 1, s);
}
bool find(string &s)
{
return find(0, s);
}
void ans()
{
vector<pair<int,int>>p;
for(int k=0; k<CHAR; k++)
{
int cur=k;
if(child[cur]!=0)
{
p.push_back({child[cur]->deep,cur});
}
}
sort(all(p));
for(int k=0; k<p.size(); k++)
{
int cur=p[k].S;
an.push_back(char(cur+'a'));
if(child[cur]->End!=0)
an.push_back('p');
child[cur]->ans();
an.push_back('-');
}
}
int CntPrefix(int i, string &s)
{
int cur = s[i] - 'a';
if (child[cur] == 0)return 0;
if (i == s.size() - 1)
{
return child[cur]->Cnt;
}
return child[cur]->CntPrefix(i + 1, s);
}
int CntPrefix(string &s)
{
return CntPrefix(0, s);
}
void clear()
{
for (int i = 0; i < CHAR; i++)
if (child[i])
child[i]->clear(), delete child[i], child[i] = 0;
}
};
int main()
{
derar
int t=1;
//cin>>t;
while(t--)
{
int n,q;
scanf("%d",&n);
TrieNode* root=new TrieNode();
for(int i=0; i<n; i++)
{
string s=getString();
int se=s.size();
ma=max(se,ma);
root->add(s,se);
}
root->ans();
cout<<an.size()-ma<<endl;
for(int i=0; i<an.size()-ma; i++)
printf("%c\n",an[i]);
}
}
Compilation message
printer.cpp: In member function 'void TrieNode::add(int, std::string&, int)':
printer.cpp:50:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
50 | if (i == s.size() - 1)
| ~~^~~~~~~~~~~~~~~
printer.cpp: In member function 'void TrieNode::erase(int, std::string&)':
printer.cpp:67:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
67 | if (i == s.size() - 1)
| ~~^~~~~~~~~~~~~~~
printer.cpp: In member function 'bool TrieNode::find(int, std::string&)':
printer.cpp:88:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | if (i == s.size() - 1)
| ~~^~~~~~~~~~~~~~~
printer.cpp: In member function 'void TrieNode::ans()':
printer.cpp:111:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for(int k=0; k<p.size(); k++)
| ~^~~~~~~~~
printer.cpp: In member function 'int TrieNode::CntPrefix(int, std::string&)':
printer.cpp:125:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
125 | if (i == s.size() - 1)
| ~~^~~~~~~~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:163:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
163 | for(int i=0; i<an.size()-ma; i++)
| ~^~~~~~~~~~~~~
printer.cpp:151:15: warning: unused variable 'q' [-Wunused-variable]
151 | int n,q;
| ^
printer.cpp: In function 'std::string getString()':
printer.cpp:22:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
22 | scanf("%s", c);
| ~~~~~^~~~~~~~~
printer.cpp: In function 'int main()':
printer.cpp:152:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
152 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2392 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
2648 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
2392 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
9 ms |
3932 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
30 ms |
8272 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
95 ms |
17692 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
187 ms |
41140 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
245 ms |
32712 KB |
didn't print every word |
2 |
Halted |
0 ms |
0 KB |
- |