当前位置:网站首页>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
边栏推荐
- Stop asking yourself if you are suitable for software testing
- 7-11 calculation of residential water charges by sections
- Accelerating strategy learning using parallel differentiable simulation
- 中感微冲刺科创板:年营收2.4亿净亏1782万 拟募资6亿
- Special research report on the market of lithium battery electrolyte industry in China (2022 Edition)
- concat和concat_ws()区别及group_concat()和repeat()函数的使用
- LNMP环境mail函数不能发送邮件解决
- Raft 协议
- 7-23 currency conversion (using array conversion)
- Exercise 10-1 judge the three digits that meet the conditions
猜你喜欢

Exercise 10-1 judge the three digits that meet the conditions

556. The next larger element III

好看、好用、强大的手写笔记软件综合评测:Notability、GoodNotes、MarginNote、随手写、Notes Writers、CollaNote、CollaNote、Prodrafts、Noteshelf、FlowUs、OneNote、苹果备忘录

Understanding of closures

泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东

Tailing rushes to the scientific and Technological Innovation Board: it plans to raise 1.3 billion, and Xiaomi Changjiang is the shareholder

Leetcode(4)——寻找两个正序数组的中位数

Exercise 10-6 recursively find Fabonacci sequence

编程语言:类型系统的本质

Protobuf and grpc
随机推荐
allegro,orcad, net alias,port,off-page connector之间的异同点和如何选取
Exercise 6-1 classify and count the number of characters
JS get DPI, PX to cm, cm to PX
7-18 finding the single root of polynomial by dichotomy
关于回溯问题中的排列问题的思考(LeetCode46题与47题)
556. 下一个更大元素 III
Selective sorting
7-7 12-24 hour system
【北大青鸟昌平校区】互联网行业中,哪些岗位越老越吃香?
一文了解微分段应用场景与实现机制
Exercise 8-7 string sorting
7-16 find the set of integers that meet the given conditions
Programming language: the essence of type system
中国锂电池电解液行业市场专项调研报告(2022版)
【7.3】146. LRU缓存机制
npm install卡住与node-npy的各种奇怪报错
Although not necessarily the best, it must be the hardest!
Mysql多表查询 #子查询
ConstraintLayout 的使用
Exercise 10-1 calculate the sum of 1 to n using recursive functions