求教教为啥上面的MLE下面的AC
查看原帖
求教教为啥上面的MLE下面的AC
1000298
Vsinger_洛天依楼主2023/12/25 16:23

rt 代码一点没改,数组开大了反而过了

#include<bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
namespace Fread{const int SIZE = (1 << 18);char buf[SIZE],*S,*T;inline char getchar() {if(S==T){T = (S = buf) + fread(buf,1,SIZE,stdin); if(S==T) return '\n';} return *S++;}}
namespace Fwrite {const int SIZE = (1 << 18);char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout), S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{ ~NTR() { flush(); }}ztr;}
#ifdef ONLINE_JUDGE
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#endif
namespace Fastio{
    struct Reader{
        template <typename T>
        Reader&operator>>(T&x){char c=getchar();bool f=false;while (c<'0'||c>'9') { if(c == '-')f = true;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}if(f)x=-x;return *this;}
        Reader&operator>>(double & x) {char c=getchar();short f=1,s=0;x=0;double t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}
        Reader&operator>>(long double&x){char c=getchar();short f=1,s=0;x=0;long double t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}
        Reader&operator>>(__float128&x){char c=getchar();short f=1,s=0;x=0;__float128 t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.') c = getchar();else return x*=f, *this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
        Reader&operator>>(char&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}
        Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return *this;}
        Reader&operator>>(string&str){char c=getchar();str.clear();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return *this;}
        template<class _Tp>
        Reader&operator>>(vector<_Tp>&vec){for(unsigned i=0;i<vec.size();i++)cin>>vec[i];return *this;}
        template<class _Tp,class _tp>
        Reader&operator>>(pair<_Tp,_tp>&a){cin>>a.first>>a.second;return *this;}
        Reader(){}
    }cin;
    const char endl='\n';
    struct Writer{
    static const int set_precision = 6;
    typedef int mxdouble;
        template<typename T>
        Writer&operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<set_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<set_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<set_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<set_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<set_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<set_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(char c){putchar(c);return*this;}
        Writer& operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
        Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
        Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed) putchar(str[st++]);return *this;}
        template<class _Tp>
        Writer&operator<<(vector<_Tp>vec){for(unsigned i=0;i<vec.size()-1;i++)cout<<vec[i]<<" ";cout<<vec[vec.size()-1];return *this;}
        template<class _Tp,class _tp>
        Writer&operator<<(pair<_Tp,_tp>a){cout<<a.first<<" "<<a.second;return *this;}
        Writer(){}
    }cout;
}
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
/* --------------- fast io --------------- */
int n,m;
char a[1000001],b[150][75];
class AC{
public:
    class Trie{
    public:
        int fail,vis[26],end; 
    }Tr[10000];
    int cnt,tot,num,val[10000],f[10000]; 
    inline void clear(){
        memset(Tr,0,sizeof(Tr));
        memset(f,0,sizeof(f));
        memset(val,0,sizeof(val));
        cnt=tot=num=0;
    }
    inline void ins(char *s){
        int l=strlen(s),q=0;
        for(int i=0;i<l;++i){
            if(!Tr[q].vis[s[i]-'a']) Tr[q].vis[s[i]-'a']=++cnt;
            q=Tr[q].vis[s[i]-'a']; 
        }
        Tr[q].end=++num;
    }
    inline void Get(){
        queue<int>Q; 
        for(int i=0;i<26;++i)
            if(Tr[0].vis[i]!=0)
                Q.push(Tr[0].vis[i]);
        while(!Q.empty()){
            int u=Q.front();
            Q.pop();
            for(int i=0;i<26;++i){
                if(Tr[u].vis[i]){
                    Tr[Tr[u].vis[i]].fail=Tr[Tr[u].fail].vis[i];
                    Q.push(Tr[u].vis[i]);
                }
                else
                    Tr[u].vis[i]=Tr[Tr[u].fail].vis[i];
            }
        }
    }
    inline int Ask(char *s){
        int l=strlen(s),q=0,ans=0;
        for(int i=0;i<l;++i){
            q=Tr[q].vis[s[i]-'a'];
            for(int t=q;t;t=Tr[t].fail)
                val[t]++;
        }
        for(int i=0;i<=cnt;++i)
            if(Tr[i].end)
                ans=max(ans,val[i]),
                f[Tr[i].end]=val[i];
        return ans;
        // int q=0,len=strlen(s),i=0;
        // while(i<len){
        //     int x=s[i]-'a';
        //     q=Tr[q].vis[x];
        //     ck.push(q);
        //     sta.push(i);
        //     // cout<<q<<" "<<i<<endl;
        //     if(Tr[q].end){
        //         for(int i=1;i<=Tr[q].end;i++){
        //             sta.pop();
        //             ck.pop();
        //         }
        //         if(sta.empty()) q=0;
        //         else q=ck.top();
        //     }
        //     i++;
        // }
    }
}AC;
signed main(){
    // freopen("1.in","r",stdin);
    // freopen("1.txt","w",stdout);
    // cerr<<"RE"<<endl;
    while(1){
        cin>>n;
        if(n==0) return 0;
        AC.clear();
        for(int i=1;i<=n;i++){
            cin>>b[i];
            AC.ins(b[i]);
        }
        cin>>a;
        AC.Get();
        int x=AC.Ask(a);
        // for(int i=1;i<=m;i++){
        //     cout<<AC.Ask(b[i])<<endl;
        // }
        cout<<x<<endl;
        for(int i=1;i<=n;i++)
            if(AC.f[i]==x)
                cout<<b[i]<<endl;
    }
}

第二次的

#include<bits/stdc++.h>
using namespace std;
#ifdef ONLINE_JUDGE
namespace Fread{const int SIZE = (1 << 18);char buf[SIZE],*S,*T;inline char getchar() {if(S==T){T = (S = buf) + fread(buf,1,SIZE,stdin); if(S==T) return '\n';} return *S++;}}
namespace Fwrite {const int SIZE = (1 << 18);char buf[SIZE],*S=buf,*T=buf+SIZE;inline void flush(){fwrite(buf,1,S-buf,stdout), S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{ ~NTR() { flush(); }}ztr;}
#ifdef ONLINE_JUDGE
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#endif
namespace Fastio{
    struct Reader{
        template <typename T>
        Reader&operator>>(T&x){char c=getchar();bool f=false;while (c<'0'||c>'9') { if(c == '-')f = true;c=getchar();}x=0;while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+(c^48);c=getchar();}if(f)x=-x;return *this;}
        Reader&operator>>(double & x) {char c=getchar();short f=1,s=0;x=0;double t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}
        Reader&operator>>(long double&x){char c=getchar();short f=1,s=0;x=0;long double t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else return x*=f,*this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}
        Reader&operator>>(__float128&x){char c=getchar();short f=1,s=0;x=0;__float128 t=0;while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.') c = getchar();else return x*=f, *this;while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return *this;}
        Reader&operator>>(char&c){c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();return *this;}
        Reader&operator>>(char*str){int len=0;char c=getchar();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return *this;}
        Reader&operator>>(string&str){char c=getchar();str.clear();while(c=='\n'||c==' '||c=='\r')c=getchar();while(c!='\n'&&c!=' '&&c!='\r')str.push_back(c),c=getchar();return *this;}
        template<class _Tp>
        Reader&operator>>(vector<_Tp>&vec){for(unsigned i=0;i<vec.size();i++)cin>>vec[i];return *this;}
        template<class _Tp,class _tp>
        Reader&operator>>(pair<_Tp,_tp>&a){cin>>a.first>>a.second;return *this;}
        Reader(){}
    }cin;
    const char endl='\n';
    struct Writer{
    static const int set_precision = 6;
    typedef int mxdouble;
        template<typename T>
        Writer&operator<<(T x){if(x==0)return putchar('0'),*this;if(x<0)putchar('-'),x=-x;static int sta[45];int top=0;while(x)sta[++top]=x%10,x/=10;while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<set_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<set_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<set_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<set_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static int sta[45];int top=0;while(_)sta[++top]=_%10,_/=10;if(!top)putchar('0');while(top)putchar(sta[top]+'0'),--top;putchar('.');for(int i=0;i<set_precision;i++)x*=10;_=x;while(_)sta[++top]=_%10,_/=10;for(int i=0;i<set_precision-top;i++)putchar('0');while(top)putchar(sta[top]+'0'),--top;return*this;}
        Writer&operator<<(char c){putchar(c);return*this;}
        Writer& operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
        Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return *this;}
        Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed) putchar(str[st++]);return *this;}
        template<class _Tp>
        Writer&operator<<(vector<_Tp>vec){for(unsigned i=0;i<vec.size()-1;i++)cout<<vec[i]<<" ";cout<<vec[vec.size()-1];return *this;}
        template<class _Tp,class _tp>
        Writer&operator<<(pair<_Tp,_tp>a){cout<<a.first<<" "<<a.second;return *this;}
        Writer(){}
    }cout;
}
#define cin Fastio :: cin
#define cout Fastio :: cout
#define endl Fastio :: endl
#define max(a,b) ((a) > (b) ? (a) : (b))
#define min(a,b) ((a) < (b) ? (a) : (b))
#endif
/* --------------- fast io --------------- */
int n,m;
char a[1000001],b[150][75];
class AC{
public:
    class Trie{
    public:
        int fail,vis[26],end; 
    }Tr[12000];
    int cnt,tot,num,val[12000],f[12000]; 
    inline void clear(){
        memset(Tr,0,sizeof(Tr));
        memset(f,0,sizeof(f));
        memset(val,0,sizeof(val));
        cnt=tot=num=0;
    }
    inline void ins(char *s){
        int l=strlen(s),q=0;
        for(int i=0;i<l;++i){
            if(!Tr[q].vis[s[i]-'a']) Tr[q].vis[s[i]-'a']=++cnt;
            q=Tr[q].vis[s[i]-'a']; 
        }
        Tr[q].end=++num;
    }
    inline void Get(){
        queue<int>Q; 
        for(int i=0;i<26;++i)
            if(Tr[0].vis[i]!=0)
                Q.push(Tr[0].vis[i]);
        while(!Q.empty()){
            int u=Q.front();
            Q.pop();
            for(int i=0;i<26;++i){
                if(Tr[u].vis[i]){
                    Tr[Tr[u].vis[i]].fail=Tr[Tr[u].fail].vis[i];
                    Q.push(Tr[u].vis[i]);
                }
                else
                    Tr[u].vis[i]=Tr[Tr[u].fail].vis[i];
            }
        }
    }
    inline int Ask(char *s){
        int l=strlen(s),q=0,ans=0;
        for(int i=0;i<l;++i){
            q=Tr[q].vis[s[i]-'a'];
            for(int t=q;t;t=Tr[t].fail)
                val[t]++;
        }
        for(int i=0;i<=cnt;++i)
            if(Tr[i].end)
                ans=max(ans,val[i]),
                f[Tr[i].end]=val[i];
        return ans;
        // int q=0,len=strlen(s),i=0;
        // while(i<len){
        //     int x=s[i]-'a';
        //     q=Tr[q].vis[x];
        //     ck.push(q);
        //     sta.push(i);
        //     // cout<<q<<" "<<i<<endl;
        //     if(Tr[q].end){
        //         for(int i=1;i<=Tr[q].end;i++){
        //             sta.pop();
        //             ck.pop();
        //         }
        //         if(sta.empty()) q=0;
        //         else q=ck.top();
        //     }
        //     i++;
        // }
    }
}AC;
signed main(){
    // freopen("1.in","r",stdin);
    // freopen("1.txt","w",stdout);
    // cerr<<"RE"<<endl;
    while(1){
        cin>>n;
        if(n==0) return 0;
        AC.clear();
        for(int i=1;i<=n;i++){
            cin>>b[i];
            AC.ins(b[i]);
        }
        cin>>a;
        AC.Get();
        int x=AC.Ask(a);
        // for(int i=1;i<=m;i++){
        //     cout<<AC.Ask(b[i])<<endl;
        // }
        cout<<x<<endl;
        for(int i=1;i<=n;i++)
            if(AC.f[i]==x)
                cout<<b[i]<<endl;
    }
}
2023/12/25 16:23
加载中...