当前位置:网站首页>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
边栏推荐
- PCB中常用快捷键
- fpga阻塞赋值和非阻塞赋值
- Exercise 10-1 judge the three digits that meet the conditions
- Strategy, tactics (and OKR)
- Tiantu investment sprint Hong Kong stocks: asset management scale of 24.9 billion, invested in xiaohongshu and Naixue
- Leetcode(4)——尋找兩個正序數組的中比特數
- 7-10 calculate salary
- 7-24 reduction of the simplest fraction (rolling Division)
- Canvas utility library fabric JS user manual
- 基因家族特征分析 - 染色体定位分析
猜你喜欢
玖逸云黑免费无加密版本源码
Similarities and differences between Allegro, OrCAD, net alias, port, off page connector and how to select them
Sub-GHz无线解决方案Z-Wave 800 系列ZG23 soc和ZGM230S模块
puzzle(016.4)多米诺效应
Exercise 6-1 classify and count the number of characters
JS input number and standard digit number are compared. The problem of adding 0 to 0
NPM install is stuck with various strange errors of node NPY
Exercise 10-6 recursively find Fabonacci sequence
Exercise 9-1 time conversion
天谋科技 Timecho 完成近亿元人民币天使轮融资,打造工业物联网原生时序数据库
随机推荐
7-4 BCD decryption (10 points)
中国PETG市场预测及战略研究报告(2022版)
Exercise 10-3 recursive implementation of exponential functions
7-9 find a small ball with a balance
Convert string to decimal integer
NPM install is stuck with various strange errors of node NPY
Leetcode (4) - - trouver la médiane de deux tableaux ordonnés positifs
Too many files with unapproved license
556. 下一个更大元素 III
Solr series of full-text search engines - basic principles of full-text search
PCB中常用快捷键
Thread. Sleep and timeunit SECONDS. The difference between sleep
Find specified characters
String reverse order
protobuf与grpc
Zabbix添加Calculated items后保存页面成空白
修改数据库中的记录为什么报这个错
puzzle(016.3)千丝万缕
泰凌冲刺科创板:拟募资13亿 国家大基金与小米长江是股东
Preliminary summary of structure