QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#54841#4187. Decrypting ZodiacSa3tElSefrTL 3ms3764kbC++2.8kb2022-10-10 20:26:592022-10-10 20:27:02

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-10-10 20:27:02]
  • 评测
  • 测评结果:TL
  • 用时:3ms
  • 内存:3764kb
  • [2022-10-10 20:26:59]
  • 提交

answer

#include <bits/stdc++.h>
#define ll long long
#define ld long double
using namespace std;

const int N = 2e5 + 5, mod = 998244353;

using cd = complex<ld>;
const ld PI = acos(-1);

void fft(vector<complex<ld>> &a, bool invert){
    int n = a.size();

    for(int i = 1, j = 0; i < n; i++){
        int bit = n >> 1;
        for(; j & bit; bit >>= 1) j ^= bit;
        j ^= bit;
        if(i < j) swap(a[i], a[j]);
    }

    for(int len = 2; len <= n; len <<= 1){
        ld ang = 2 * PI / len;
        if(invert) ang *= -1;
        cd wlen(cos(ang), sin(ang));
        for(int i = 0; i < n; i += len){
            cd w(1);
            for(int j = 0; j < len / 2; j++){
                cd u = a[i + j], v = a[i + j + len / 2] * w;
                a[i + j] = u + v;
                a[i + j + len / 2] = u - v;
                w *= wlen;
            }
        }
    }

    if(invert){
        for(auto &i: a) i /= n;
    }

}
vector<complex<ld> > addition(vector<complex<ld> > fa, vector<complex<ld> > fb){
    for(int i = 0; i < fa.size(); i++) fa[i] += fb[i];
    return fa;
}
vector<complex<ld> > multiply(vector<complex<ld> > fa, vector<complex<ld> > fb){
    for(int i = 0; i < fa.size(); i++) fa[i] *= fb[i];
    return  fa;
}

vector<vector<int> > a(26), b(26);
vector<vector<complex<ld> > > fa(26), fb(26);

vector<int> get(string &s, char c) {
    int n = s.size();
    vector<int> ret(n, 0);
    for(int i = 0; i < n; i++) {
        if(s[i] == c) {
            ret[i] = 1;
        }
    }
    return ret;
}

int n;

int solve(int shift) {

    vector<complex<ld>> res;
    for(int i = 0, j = shift; i < 26; i++, j++) {
        if(j == 26) j = 0;
        if(i == 0) {
            res = multiply(fa[i], fb[j]);
        }   else {
            res = addition(res, multiply(fa[i], fb[j]));
        }
    }

    fft(res, 1);
    vector<int> vals;
    for(auto x : res)vals.push_back((int) round(x.real()));
    int ans = n;
    for(int i = n - 1; i < n - 1 + n; i++) {
        ans = min(ans, n - vals[i]);
    }
    return ans;

}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    string s, t;
    cin >> n >> s >> t;
    s += s;
    reverse(t.begin(), t.end());


    for(char c = 'a'; c <= 'z'; c++) {
        a[c - 'a'] = get(s, c);
        b[c - 'a'] = get(t, c);
    }

    int ans = n;
    int n = 1;
    while (n < a[0].size() + b[0].size()) n <<= 1;

    for(int i = 0; i < 26; i++) {
        fa[i] = vector<complex<ld>>(a[i].begin(),a[i].end());
        fb[i] = vector<complex<ld>>(b[i].begin(),b[i].end());
        fa[i].resize(n);
        fb[i].resize(n);
        fft(fa[i], 0);
        fft(fb[i], 0);
    }

    for(int i = 0; i < 26; i++) {
        ans = min(ans, solve(i));
    }

    cout << ans;




    return 0;
}





Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 3636kb

input:

6
drhmex
zodiac

output:

2

result:

ok single line: '2'

Test #2:

score: 0
Accepted
time: 3ms
memory: 3728kb

input:

8
dicepara
paradise

output:

1

result:

ok single line: '1'

Test #3:

score: 0
Accepted
time: 3ms
memory: 3764kb

input:

13
lvlvdvdqsonwk
thisisasample

output:

2

result:

ok single line: '2'

Test #4:

score: -100
Time Limit Exceeded

input:

150000
ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc...

output:


result: