TLE on #10,#11
查看原帖
TLE on #10,#11
936864
liaowenqiaa楼主2025/1/25 19:37

rt

#include<bits/stdc++.h>
using namespace std;
#define maxn 1001
bitset<2000002> vis;
string s;
int n, m,zans;

inline void read(string &s) {
    s = "";
    char c = getchar_unlocked();
    while (c < 'a' || c > 'z') c = getchar_unlocked();
    while (c >= 'a' && c <= 'z')
        s += c, c = getchar_unlocked();
}

inline void write(int x) {
    if (x < 10) return putchar_unlocked(x + '0'), void();
    write(x / 10);
    putchar_unlocked(x % 10 + '0');
}

struct trie {
    bool end[maxn];
    int cnt,nex[maxn][26];
} t;

int max(int x,int y){
    return x>y?x:y;
}

void update() {
    int id=0,len=s.size();
    for(int i=0; i<len; i++) {
        int u=s[i]-'a';
        if(t.nex[id][u]==0) t.nex[id][u]=++t.cnt;
        id=t.nex[id][u];
    }
    t.end[id]=1;
}

void query(int ik,int len) {
    if(vis[ik]) return;
    vis[ik]=1;
    if(zans<ik) zans=ik;
    int i,id=0;
    for(i=ik;i<len;i++){
        int u=s[i]-'a';
        if(t.nex[id][u]==0) break;
        id=t.nex[id][u];
        if(t.end[id]==1) query(i+1,len);
    }
}

int main() {
	cin>>n>>m;
    for(int i=1; i<=n; i++) {
        read(s);
        update();
    }
    while(m--) {
    	vis.reset();
        zans=0;
        read(s);
		query(0,s.size());
        write(zans);
        putchar('\n');
    }
    return 0;
}
2025/1/25 19:37
加载中...