QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#745903#8252. Tip of Your TonguerahulmnavneethAC ✓3313ms215116kbC++174.5kb2024-11-14 12:20:362024-11-14 12:20:37

Judging History

你现在查看的是最新测评结果

  • [2024-11-14 12:20:37]
  • 评测
  • 测评结果:AC
  • 用时:3313ms
  • 内存:215116kb
  • [2024-11-14 12:20:36]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define ld long double
#define EPS 1e-9
#define all(x) x.begin(),x.end()
#define allr(x) x.rbegin(),x.rend()
#define yes cout << "YES" << '\n';
#define no cout << "NO" << '\n';
#define mem(arrr, xx) memset(arrr,xx,sizeof arrr)
#define endl '\n'
#define PI acos(-1)
#define Ones(n) __builtin_popcount(n)
#define Onesl(n) __builtin_popcountll(n)
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
const ll MOD = 1e9 + 7;
//const ll N = 2e5 + 5;
const int OO = 0x3f3f3f3f;
const ll LOO = 0x3f3f3f3f3f3f3f3f;
int dx8[] = {+0, +0, -1, +1, +1, +1, -1, -1};
int dy8[] = {-1, +1, +0, +0, +1, -1, +1, -1};
int dx4[] = {+0, +0, -1, +1};
int dy4[] = {-1, +1, +0, +0};

void Rokba() {
    cin.tie(nullptr), cout.tie(nullptr), cin.sync_with_stdio(false), cout.sync_with_stdio(false);
#ifdef Clion
    freopen("input.txt", "r", stdin), freopen("output.txt", "w", stdout);
#endif
}


const int N = 1e6 + 5, P1 = 31, P2 = 37, M = 1e9 + 7;
int pw1[N], pw2[N], inv1[N], inv2[N];

int mul(int a, int b){
    a = ((a % M) + M) % M;
    b = ((b % M) + M) % M;
    return (a * 1LL * b) % M;
}

int add(int a, int b){
    a = ((a % M) + M) % M;
    b = ((b % M) + M) % M;
    return (a + b)%M;
}

int fastPower(int base, int power){
    if(!power)return 1;
    int ret = fastPower(base, power >> 1);
    ret = mul(ret, ret);
    if(power % 2) ret = mul(ret, base);
    return ret;
}

void pre(){
    pw1[0] = inv1[0] = pw2[0] = inv2[0] = 1;
    int mulInv1 = fastPower(P1, M - 2);
    int mulInv2 = fastPower(P2, M - 2);
    for (int i = 1; i < N; ++i) {
        pw1[i] = mul(pw1[i - 1], P1);
        pw2[i] = mul(pw2[i - 1], P2);
        inv1[i] = mul(inv1[i - 1], mulInv1);
        inv2[i] = mul(inv2[i - 1], mulInv2);
    }
}

struct Hash{
    vector<pair<int, int>> prefixHash;

    Hash(){

    }

    Hash(string s){
        prefixHash = vector<pair<int, int>>(s.size(), {0, 0});
        for (int i = 0; i < s.size(); ++i) {
            prefixHash[i].first = mul(s[i] - 'a' + 1, pw1[i]);
            prefixHash[i].second = mul(s[i] - 'a' + 1, pw2[i]);
            if(i){
                prefixHash[i] = {
                        add(prefixHash[i].first, prefixHash[i - 1].first),
                        add(prefixHash[i].second, prefixHash[i - 1].second)
                };
            }
        }
    }

    pair<int, int> getHashVal(){
        return prefixHash.back();
    }

    pair<int, int> getRangeHashVal(int l, int r){
        return {
                mul(add(prefixHash[r].first, -(l ? prefixHash[l - 1].first : 0)), inv1[l]),
                mul(add(prefixHash[r].second, -(l ? prefixHash[l - 1].second : 0)), inv2[l])
        };
    }
};

typedef pair<int, int> pi;

void Solution() {
    int n, q;
    cin >> n >> q;
    vector<string> v(n);
    map<pi, int> pref, suf;
    map<pair<pi, pi>, int> both;
    for (int i = 0; i < n; ++i) {
        cin >> v[i];
        Hash h(v[i]);
        for (int j = 0, k = (int)v[i].size() - 1; j < v[i].size(); ++j, k--) {
            pref[h.getRangeHashVal(0, j)]++;
            suf[h.getRangeHashVal(k, (int)v[i].size() - 1)]++;
            both[{h.getRangeHashVal(0, j), h.getRangeHashVal(k, (int)v[i].size() - 1)}]++;
        }
    }
    while (q--){
        string type, a, b;
        cin >> type >> a >> b;
        Hash pr(a), sf(b);
        int ans = 0;
        if(type == "AND"){
            if(both.count({pr.getHashVal(), sf.getHashVal()})){
                ans += both[{pr.getHashVal(), sf.getHashVal()}];
            }
        }
        else if(type == "OR"){
            if(pref.count(pr.getHashVal())){
                ans += pref[pr.getHashVal()];
            }
            if(suf.count(sf.getHashVal())){
                ans += suf[sf.getHashVal()];
            }
            if(both.count({pr.getHashVal(), sf.getHashVal()})){
                ans -= both[{pr.getHashVal(), sf.getHashVal()}];
            }
        }
        else{
            if(pref.count(pr.getHashVal())){
                ans += pref[pr.getHashVal()];
            }
            if(suf.count(sf.getHashVal())){
                ans += suf[sf.getHashVal()];
            }
            if(both.count({pr.getHashVal(), sf.getHashVal()})){
                ans -= 2 * both[{pr.getHashVal(), sf.getHashVal()}];
            }
        }
        cout << ans << '\n';
    }
}

int main() {
    Rokba();
    pre();
    int T = 1;
//    cin >> T;
    while (T--) {
        Solution();
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 10ms
memory: 19256kb

input:

4 4
cat
catcat
octal
occidental
AND cat cat
OR oc at
AND ca at
XOR oc al

output:

2
4
2
0

result:

ok 4 lines

Test #2:

score: 0
Accepted
time: 10ms
memory: 19200kb

input:

26 36
a
b
c
d
e
f
g
h
i
j
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
z
AND b b
AND d d
XOR tk ce
AND w w
AND z z
XOR t t
OR a a
OR s s
AND p p
AND v v
AND pp kh
XOR j j
AND n n
OR f f
XOR vo mj
OR m m
XOR q q
XOR r r
AND i i
OR l l
OR jb cg
XOR x x
XOR nf ov
OR e e
XOR h h
XOR u u
XOR c c
OR y y
XOR nc ln
AND o ...

output:

1
1
0
1
1
0
1
1
1
1
0
0
1
1
0
1
0
0
1
1
0
0
0
1
0
0
0
1
0
1
0
0
0
0
1
1

result:

ok 36 lines

Test #3:

score: 0
Accepted
time: 474ms
memory: 54616kb

input:

18 18
a
ab
abba
abbabaab
abbabaabbaababba
abbabaabbaababbabaababbaabbabaab
abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababba
abbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaab
abbabaabbaababbabaababbaabbabaa...

output:

18
17
8
8
14
7
6
11
10
5
4
3
3
2
2
0
1
0

result:

ok 18 lines

Test #4:

score: 0
Accepted
time: 3313ms
memory: 215116kb

input:

1 1
ocyzfhhambhirbkeodnshorawhzsqakgywcadicyacxleobjmjrbgqrdqqvqecccxuahrkwnimglrcmiaujynldydopyasdbefpxmagfquhzrtfnuikojwpjocjwrogxhrquqruqqsunsjotsgeetddviaoswcavdswftyheurrclunactnwqhnqzrxjlsipyxxmmeiwxawqzvhtcmmadyvfzrhinphlibltwartaczraqkaaljefkksbawqmnsbquqxpbneshiuypfafihqtehavpdsoauwyvsqblxo...

output:

0

result:

ok single line: '0'

Test #5:

score: 0
Accepted
time: 23ms
memory: 28032kb

input:

1 1
a
OR zxuxomuitrpatgdidwnuxfrwultbimmesgzkgcdsvjzmanhyebwdzowthmiqlsagxoqvyciyslmoxvceppnkjuvzhszgcdnpmdqxhbinbknbbzfcnhizysyeaemkpeandwqutpmnxytmnkzfghmptiqnppuwndlxlhpimvmsaytdycezpodqcbddybswutmimtpgzhmijvpphelairwewqshektuldufwxuzirjkpemoafnpopqfgxefcxuzgbbhehfpbmkdqbxsdejspcydluchncbhbfghoys...

output:

0

result:

ok single line: '0'

Test #6:

score: 0
Accepted
time: 945ms
memory: 74728kb

input:

250 50000
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

1
250
249
1
1
1
1
250
250
1
1
250
249
1
1
249
250
250
1
250
250
1
249
1
1
249
1
1
1
250
249
250
250
250
250
1
250
249
249
249
1
250
1
250
249
249
250
249
250
250
1
250
250
250
250
250
1
1
249
249
250
1
1
250
1
250
249
250
1
249
1
249
1
249
1
1
1
250
250
249
249
250
250
1
250
249
250
1
1
250
1
250
25...

result:

ok 50000 lines

Test #7:

score: 0
Accepted
time: 1114ms
memory: 80608kb

input:

10 10
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

output:

10
10
0
10
10
10
10
0
0
10

result:

ok 10 lines

Test #8:

score: 0
Accepted
time: 455ms
memory: 54152kb

input:

10000 40000
lhheuleivzyldlchherj
phgmehaliwwulthedmop
ifszbvkgywuyemcxbvns
wycojxbttxudiiclvgvk
camfcfwvxlvyoyfyqsci
zqkxtcfhovfemzizxhev
ugbsvnxratulswilmekh
cvveyzynvzgnfukgorio
lsstnqurowhzvvzmvhwh
crhuqauvnvwdeyqlxchi
xvbybzkvzkjntwtdiglt
ylgvjsugwaicedszzhlv
jyswsdydzgicxheoqjfh
jnbmcjgbyfmxyae...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 40000 lines

Test #9:

score: 0
Accepted
time: 2455ms
memory: 156216kb

input:

40000 10000
lynhbrgdcbqmazwibcti
xxbyaqngttjaadowpoxd
jyxvinberwqbykawbyvf
vayqjzfyjlvzsuwwrsvs
lukgakfkiiaxuedhtzpx
uwhoadeqqzhkwggfebkj
uyfreqcgwgtqvqlwitzf
zajebujpbhufhvyowsbh
sbnvcyswptsbbdvdxsxl
oozymjrfnjogbxlbptbw
vlxvuhfueomwhmjcddlc
szffcnpiwpgrymdqimnp
cesclsyvliesdrhoulim
ceryoczqhltomnm...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 10000 lines

Test #10:

score: 0
Accepted
time: 1108ms
memory: 105320kb

input:

25000 25000
vdyrdfobpnmzkafujwav
sejhjjarcfatdrxcdhxg
tgvgnooftnykcynfbyxs
xtzqsasggmfouxwbsnhz
npwdngyaxjblnlqvgiqu
ivcevcmhsezxcqyhihvt
ilrztetzikmehuztbghe
vosgdubogglelqdgjsyf
gagqyoscjfirtpsdpcjy
wdrhtcvbelqkxxiymjif
iukqdbdggkpjftahbsuq
huvznzywxqpmpfnrjgkc
ieezfelxxquvridxswbl
vhtrgplvqgewjhk...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 25000 lines

Test #11:

score: 0
Accepted
time: 916ms
memory: 93956kb

input:

50000 50000
fhbzhhsbzj
iplzfbimxp
plobcikqkd
amxzydjuwi
xiublxnnoi
cpnwnulhjz
cpspkovuot
dvkiqopibs
deehvpeijn
cueklrdhay
ubcoawbhvd
irzkcpnyhv
yehoeolcbq
shxsfaekgn
ynqdlcblvc
avexwjuolf
oabfylgpjm
xkkqgisphg
jiokzejjof
ryobskxxtu
rotajeixrg
ctvimviwgz
smbimudyle
olnerzqovj
woslasgtbg
pewowwuuth
th...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
...

result:

ok 50000 lines

Test #12:

score: 0
Accepted
time: 2254ms
memory: 170184kb

input:

20 20
gddaatgysreztfnjttlpsfhskbxjqltiokdxifltzipusrztchtejvziswzynfyytotyefjuseowxnakvzvzecyfxstsbtegvmtzjsdaixnnjvoyrqfmvnqcgiqznzzyosolumqhzuhknstpsindhxfctphufyebubprvlxoqmtfsbcnehyjewncdmiyxxrotihfxvdkinwtbtfqyncrypmbrbevsvofswrfvcuzcpwdaxsxjqgluuvvsteaobdlnlwboswqmvbexhpxzmktadrtwvcsdpfyluwhht...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 20 lines

Test #13:

score: 0
Accepted
time: 1033ms
memory: 113992kb

input:

20 20
vieddzryrbrgvezcrdvtfujyllhusrcemmxlarpmwccaeuvjnxecjxyxhhilaiyqtisyxxtfjnlzvepxiivavycywkkxdrmifgxggcupbbkvlymnrrvfaauoxieymchvdyravgowdvaoqjnbojfpgmtieydackvuruzhnmcblxlmiauyrpmbntnejwprfoeylbpzrpnjoqxpafdkfrnhfcjtbzpekpxfnwvjkpnvdlfbvrblsysybxpeonxwaqaiuqswbfialryoxezgoijbktvrisrrgrndkmtibl...

output:

0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0
0

result:

ok 20 lines

Test #14:

score: 0
Accepted
time: 1214ms
memory: 113804kb

input:

10 10
fxbapnhzxqgypinkcwvcdffncpofscfumrhdywwsndelaxgwwintmtrbwbfkmycuuofnpkxusaxushqptczpkvjzutdgjbjubkvwopzvduvinmixbozzyuwnozuwemctztrcyqdwqejqbnvrsuhoherqgjdxctzfrdzysinwauupiealofgezefjzubjycpxswstyqcohmllwtfmrdtqakfgopirtyjlwniezmllpfcwloczdfgtwkxumffysdtpiclykobzoqqzprsjqjjfkcuaeokdxhhqgnxjpb...

output:

0
0
0
0
0
0
0
0
0
0

result:

ok 10 lines

Test #15:

score: 0
Accepted
time: 647ms
memory: 76828kb

input:

20000 15000
ycpcujwskbndpkstgaznlbryxji
ycpcujwskbdseynvaaznlbryxji
ycpcujwskbygsrainaznlbryxji
ycpcujwskbnatcrctaznlbryxji
ycpcujwskbkaxnsymaznlbryxji
ycpcujwskbtpqpibsaznlbryxji
ycpcujwskbhmudkxraznlbryxji
ycpcujwskbueqmfflaznlbryxji
ycpcujwskbfkeudapaznlbryxji
ycpcujwskbjxnhhhsaznlbryxji
ycpcujws...

output:

20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
20000
...

result:

ok 15000 lines

Test #16:

score: 0
Accepted
time: 28ms
memory: 20852kb

input:

10 2700
jmldwyxnouejzaqmujxahyissokibdqbgvqzauolmrjbzuibrkncaxfeepypxavmwqfntjzqszisvunypogzuiczxiaqkjlyzhmzirbnrsibwgyiqbvdqfccztiepuihkdqutzoctwnxfgixmawyyldzxrrtclpujhywsyidnetozusfvsfyrhfqfhufvlcwfwovdrlkczeinjygyjlijrygjplsssqhobmxdrgotnmwwwekgompkmfwjmvwgcsfrmmmlybrwcgnbsbxwpthpuwynlefetsygzln...

output:

10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
...

result:

ok 2700 lines

Test #17:

score: 0
Accepted
time: 28ms
memory: 20928kb

input:

10 2700
otckkjfmnhodtkatewfpeyxqurahqazyormsrsxhdioocfgslbxyzcnbiyvsgfxpfqcxkifjnzfuvhpytvyzqhnspltgahsczsnhdwbgwtfwjdhyisufyqupenclrlxwebpipopfmbpdjwelmiiocjddgyihlomaetukpbxxqpvjvqkfhupzhwdwdyshekxagurryjzaealcvqsomigcihlxtlgbiyutuqskwoofnqofgzkkgoncmkhjgijtlxfjewsampxorudxhlctcfspudtkrhnyexcyeoli...

output:

10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
10
...

result:

ok 2700 lines