QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#343108 | #8252. Tip of Your Tongue | ElTeeran_ElHayga# | AC ✓ | 2852ms | 215024kb | C++20 | 4.5kb | 2024-03-01 22:30:24 | 2024-03-01 22:30:24 |
Judging History
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();
}
}
详细
Test #1:
score: 100
Accepted
time: 8ms
memory: 19420kb
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: 8ms
memory: 19176kb
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: 430ms
memory: 54704kb
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: 2852ms
memory: 215024kb
input:
1 1 ocyzfhhambhirbkeodnshorawhzsqakgywcadicyacxleobjmjrbgqrdqqvqecccxuahrkwnimglrcmiaujynldydopyasdbefpxmagfquhzrtfnuikojwpjocjwrogxhrquqruqqsunsjotsgeetddviaoswcavdswftyheurrclunactnwqhnqzrxjlsipyxxmmeiwxawqzvhtcmmadyvfzrhinphlibltwartaczraqkaaljefkksbawqmnsbquqxpbneshiuypfafihqtehavpdsoauwyvsqblxo...
output:
0
result:
ok single line: '0'
Test #5:
score: 0
Accepted
time: 19ms
memory: 28236kb
input:
1 1 a OR zxuxomuitrpatgdidwnuxfrwultbimmesgzkgcdsvjzmanhyebwdzowthmiqlsagxoqvyciyslmoxvceppnkjuvzhszgcdnpmdqxhbinbknbbzfcnhizysyeaemkpeandwqutpmnxytmnkzfghmptiqnppuwndlxlhpimvmsaytdycezpodqcbddybswutmimtpgzhmijvpphelairwewqshektuldufwxuzirjkpemoafnpopqfgxefcxuzgbbhehfpbmkdqbxsdejspcydluchncbhbfghoys...
output:
0
result:
ok single line: '0'
Test #6:
score: 0
Accepted
time: 731ms
memory: 74680kb
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: 1018ms
memory: 80660kb
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: 325ms
memory: 53840kb
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: 2012ms
memory: 156176kb
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: 1176ms
memory: 105208kb
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: 1177ms
memory: 93948kb
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: 2655ms
memory: 169996kb
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: 1384ms
memory: 113896kb
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: 1623ms
memory: 113956kb
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: 821ms
memory: 76796kb
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: 22ms
memory: 20676kb
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: 34ms
memory: 20676kb
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