首页 > 其他分享 >【字典树】Atcoder Beginner Contest 287----E

【字典树】Atcoder Beginner Contest 287----E

时间:2023-01-29 18:44:56浏览次数:76  
标签:Atcoder typedef const Beginner Contest int long include define

题目:E - Karuta (atcoder.jp)

题解:这道题就是一个字典树求最长公共前缀的板子题。可以直接交板子。

但我在翻题解的时候发现了另一种思路,就是将字符串按字典序排列后,每一个字符串的LCA(最长公共前缀)一定是和相邻两个字符串的LCA中的一个。

板子做法:

#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define endl '\n'
using namespace std;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pll;
typedef unsigned long long ULL;
const ll mod = 1e9+7;
const int N = 5e5 + 5;
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
int cmp(int a, int b)
{
    return a > b;
}
int son[N][26],cnt[N],idx;

string s[N];
void insert(string s,int v)
{
    int p=0;
    for(int i=0;i<s.length();i++)
    {
        cnt[p]+=v;
        int u=s[i]-'a';
        if(!son[p][u]) son[p][u]=++idx;
        p=son[p][u];
    }
    cnt[p]+=v;
}
int query(string s)
{
    int p=0;
    int ans=0;
    for(int i=0;i<s.length();i++)
    {
        int u=s[i]-'a';
        if(!son[p][u]) return ans;
        p=son[p][u];
        if(cnt[p]==0) return ans;
        ans++;
    }
    return ans;
}
void slove()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>s[i];
        insert(s[i],1);
    }
    for(int i=1;i<=n;i++)
    {
        insert(s[i],-1);
        cout<<query(s[i])<<endl;
        insert(s[i],1);
    }
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    slove();
    return 0;
}

第二种做法:

#include <bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
#define ll long long
#define endl '\n'
using namespace std;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pll;
typedef unsigned long long ULL;
const ll mod = 1e9+7;
const int N = 5e5 + 5;
int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}
int cmp(int a, int b)
{
    return a > b;
}
struct node{
    string s;
    int id;
    bool operator<(const node&t)const{
        return s<t.s; 
    }
}a[N];
int sum(int x,int y)
{
    int res=0;
    string s1=a[x].s,s2=a[y].s;
    int len=min(s1.size(),s2.size());
    for(int i=0;i<len;i++)
    {
        if(s1[i]!=s2[i]) break;
        res++;
    }
    return res;
}
void slove()
{
    int n;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i].s;
        a[i].id=i;
    }
    sort(a+1,a+1+n);
    int ans[N];
    memset(ans,0,sizeof(ans));
    for(int i=1;i<=n;i++)
    {
        int maxn=0;
        if(i-1>0&&i-1<=n)
        {
            maxn=max(maxn,sum(i,i-1));
        }
        if(i+1>0&&i+1<=n)
        {
            maxn=max(maxn,sum(i,i+1));
        }
        ans[a[i].id]=maxn;
    }
    for(int i=1;i<=n;i++) cout<<ans[i]<<endl;
}
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0), cout.tie(0);
    slove();
    return 0;
}

  

标签:Atcoder,typedef,const,Beginner,Contest,int,long,include,define
From: https://www.cnblogs.com/hhhhy0420/p/17073590.html

相关文章

  • AtCoder Beginner Contest 287 解题报告
    AtCoderBeginnerContest287解题报告\(\text{ByDaiRuiChen007}\)ContestLinkA.Majority用map分别统计两种字符串出现的次数并与\(\left\lfloor\dfracn2\righ......
  • AtCoder Beginner Contest 287
    A-Majority(abc287a)题目大意给定\(n\)个人对某个提案的意见,问大多数意见是支持还是反对解题思路统计比较即可。神奇的代码#include<bits/stdc++.h>usingnam......
  • AtCoder Beginner Contest 287
    纯纯手速场C首先这张图必须是一棵树,必有\(M=N-1\)。接下来只需求出树的直径,判断其长度(边数)是否为\(N-1\)即可。https://atcoder.jp/contests/abc287/submissions/3......
  • Atcoder Beginner Contest 287
    赛时吃了三个法师,不过问题不大。赛时AB简单字符串处理。C中需要满足:\(m=n-1\)只有两个度数为\(1\)的点,剩下点的度数都为\(2\)。记得判连通!!D根据题目要求观......
  • AtCoder Beginner Contest 130
    AtCoderBeginnerContest130https://atcoder.jp/contests/abc130补补之前的A-Rounding#include<bits/stdc++.h>usingnamespacestd;intmain(){inta......
  • Atcoder ABC244E - King Bombee 题解
    原题:AtcoderABC244E-KingBombee题意给你一张图,从\(S\)到\(T\),经过\(k\)条边,经过\(X\)号点偶数次的方案数。做法设\(f_{i,j,k}\)表示经过\(i\)条边,......
  • AtCoder Beginner Contest 159
    A-TheNumberofEvenPairs相加为偶数只有偶加偶和奇加奇两种情况,其实就是在\(n\)个数中取两个(\(C\binom{2}{n}\)),在\(m\)个数中取两个(\(C\binom{2}{m}\))。B-String......
  • AtCoder Beginner Contest 126
    AtCoderBeginnerContest126https://atcoder.jp/contests/abc126A-ChangingaCharacter#include<bits/stdc++.h>usingnamespacestd;intmain(){int......
  • AtCoder Beginner Contest 162
    A-Lucky7大水题,模拟即可。#include<iostream>#include<cstdio>usingnamespacestd;intmain(){strings;cin>>s;if(s[0]=='7'||s[1]==......
  • AtCoder Beginner Contest 172
    A-Calc(abc172a)题目大意给定一个\(a\),输出\(a+a^2+a^3\)解题思路模拟即可。神奇的代码#include<bits/stdc++.h>usingnamespacestd;usingLL=long......