当前位置:网站首页>Luogu p3065 [usaco12dec]first! G problem solution
Luogu p3065 [usaco12dec]first! G problem solution
2022-07-03 14:27:00 【q779】
Luogu P3065 [USACO12DEC]First! G Answer key
Topic link :P3065 [USACO12DEC]First! G
The question :
Bessie I've been studying strings . She found that , By changing the order of the alphabet , She can arrange strings according to the changed alphabet ( Dictionary order, size order ).
for example ,Bessie Find out , For character strings “omm”,“moo”,“mom” and “ommnom”, She can use the standard alphabet “mom” In the first place ( That is, the dictionary order is the smallest ), She can also use the alphabet “abcdefghijklonmpqrstuvwxyz” bring “omm” In the first place . However ,Bessie I can't think of any way ( Change the order of the alphabet ) bring “moo” or “ommnom” In the first place .
Next, let's reorder the alphabet to figure out which strings are first in the input ( That is, the dictionary order is the smallest ), To help Bessie.
To compute a string X And string Y A rearranged order of letters , First find their first different letter X[i] And Y[i], Compare in alphabetical order after rearrangement , if X[i] Than Y[i] First , be X The dictionary order of Y Small , namely X be ranked at Y front ; If there are no different letters , Then compare X And Y length , if X Than Y short , be X The dictionary order of Y Small , namely X be ranked at Y front .
∑ ∣ S i ∣ ≤ 3 × 1 0 5 \sum \left|S_i\right| \le 3 \times 10^5 ∑∣Si∣≤3×105
obviously trie Tree save string
For each string , We all assume that it can be a character with the minimum lexicographic order
Then traverse each of its characters in turn , Make them the smallest possible
Concrete , For characters u u u , If we want it to be better than v v v The dictionary order is small ,
We can u u u towards v v v Even a directed side
Obviously, the situation without solution will form a ring , This can be used topo To judge
It is worth noting that , If the substring of a string also exists in the input data , Then it must have no solution
Time complexity O ( 26 × ∑ ∣ S i ∣ ) O(26 \times \sum |S_i|) O(26×∑∣Si∣)
Code :
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <queue>
using namespace std;
#define int long long
#define INF 0x3f3f3f3f3f3f3f3f
#define N (int)(3e4+15)
#define M (int)(3e5+15)
int n,ans;
string s[N];
bool ok[N];
struct Trie
{
int tot,trie[M][26],ed[M],e[35][35],in[26];
queue<int> q;
void insert(string str)
{
int u=0;
for(int i=0; i<str.size(); i++)
{
int c=str[i]-'a';
if(!trie[u][c])trie[u][c]=++tot;
u=trie[u][c];
}
ed[u]=1;
}
void topo()
{
while(!q.empty())q.pop();
for(int i=0; i<26; i++)
if(!in[i])q.push(i);
while(!q.empty())
{
int u=q.front();q.pop();
for(int c=0; c<26; c++)
if(e[u][c]&&(!(--in[c])))
q.push(c);
}
}
bool solve(string str)
{
int u=0;
memset(e,0,sizeof(e));
memset(in,0,sizeof(in));
for(int i=0; i<str.size(); i++)
{
if(ed[u])return 0;
int c=str[i]-'a';
for(int j=0; j<26; j++)
{
if(j!=c&&trie[u][j]&&!e[c][j])
e[c][j]=1,++in[j];
}
u=trie[u][c];
}
topo();
for(int i=0; i<26; i++)
if(in[i])return 0;
return 1;
}
}tr;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
// freopen("check.in","r",stdin);
// freopen("check.out","w",stdout);
cin >> n;
for(int i=1; i<=n; i++)
{
cin >> s[i];
tr.insert(s[i]);
}
for(int i=1; i<=n; i++)
if(tr.solve(s[i]))
{
++ans;
ok[i]=1;
}
cout << ans << '\n';
for(int i=1; i<=n; i++)
if(ok[i])cout << s[i] << '\n';
return 0;
}
Reprint please explain the source
边栏推荐
- 常见问题之PHP——ldap_add(): Add: Undefined attribute type in
- 剑指 Offer 28. 对称的二叉树
- puzzle(016.3)千丝万缕
- 7-23 currency conversion (using array conversion)
- Frequently asked questions: PHP LDAP_ add(): Add: Undefined attribute type in
- Mongodb index
- Common commands for getting started with mongodb database
- 如何查询淘宝天猫的宝贝类目
- Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
- Learn to punch in today
猜你喜欢
7-8 overspeed judgment
JS input number and standard digit number are compared. The problem of adding 0 to 0
玖逸云黑免费无加密版本源码
7-9 find a small ball with a balance
Exercise 10-1 calculate the sum of 1 to n using recursive functions
必贝特医药冲刺科创板:年营收97万亏损1.37亿 拟募资20亿
Fabric. JS document
Programming language: the essence of type system
puzzle(016.4)多米诺效应
Jiuyi cloud black free encryption free version source code
随机推荐
Exercise 8-8 moving letters
MongoDB索引
ConstraintLayout 的使用
FPGA blocking assignment and non blocking assignment
Understand the application scenario and implementation mechanism of differential segment
中国PETG市场预测及战略研究报告(2022版)
中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
C language,%d% Difference between 2D%2d%02d
Mysql多表查询 #子查询
ShowMeBug入驻腾讯会议,开启专业级技术面试时代
Although not necessarily the best, it must be the hardest!
JS matrix zero
7-9 find a small ball with a balance
Exercise 10-3 recursive implementation of exponential functions
7-15 calculation of PI
fpga阻塞赋值和非阻塞赋值
一文了解微分段应用场景与实现机制
Sendmail can't send mail and it's too slow to send. Solve it
Strategy, tactics (and OKR)
Exercise 10-1 judge the three digits that meet the conditions