#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);
| ~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2392 KB |
Output is correct |
2 |
Correct |
5 ms |
3160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
4016 KB |
Output is correct |
2 |
Correct |
12 ms |
4344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
8524 KB |
Output is correct |
2 |
Correct |
66 ms |
15440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
17696 KB |
Output is correct |
2 |
Correct |
102 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
203 ms |
40904 KB |
Output is correct |
2 |
Correct |
278 ms |
91584 KB |
Output is correct |
3 |
Correct |
243 ms |
48076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
32460 KB |
Output is correct |
2 |
Correct |
323 ms |
108600 KB |
Output is correct |
3 |
Correct |
264 ms |
54480 KB |
Output is correct |
4 |
Correct |
313 ms |
102604 KB |
Output is correct |