답안 #156439

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
156439 2019-10-05T16:28:41 Z jh05013 원형 문자열 (IZhO13_rowords) C++17
100 / 100
195 ms 548 KB
#include <bits/stdc++.h>
#define entire(X) X.begin(),X.end()
using namespace std; typedef long long ll;
void OJize(){cin.tie(NULL); ios_base::sync_with_stdio(false);}
template <class T1, class T2>ostream&operator<<(ostream &os,pair<T1,T2>const&x){os<<'('<<x.first<<", "<<x.second<<')';return os;}
template <class Ch, class Tr, class Container>basic_ostream<Ch,Tr>&operator<<(basic_ostream<Ch,Tr>&os,Container const&x){for(auto&y:x)os<<y<<" ";return os;}

#define GET(arr, x) (((arr[x >> 6] >> (x & 63)) & 1) == 1)
#define SET(arr, x) (arr[x >> 6] |= 1llu << (x & 63))
typedef unsigned long long ull;
int lcs(string A, string B){
    int N = A.size(), M = B.size(), sz = (M>>6)+1;
    vector<vector<ull>> S(256, vector<ull>(sz));
    for(int j=0; j<M; j++) SET(S[B[j]], j);
    vector<ull> row(sz);
    for(int j=0; j<M; j++) if(A[0] == B[j]){SET(row, j); break;}
    for(int i=1; i<N; i++){
        ull shl_carry = 1, minus_carry = 0;
        for(int k=0; k<sz; k++){
            ull x_k = S[A[i]][k] | row[k];
            ull term_1 = (row[k] << 1) | shl_carry;
            shl_carry = row[k] >> 63;
            auto sub_carry = [](ull &x, ull y){
                ull tmp = x; return (x = tmp-y) > tmp;
            };
            ull term_2 = x_k;
            minus_carry = sub_carry(term_2, minus_carry);
            minus_carry += sub_carry(term_2, term_1);
            row[k] = x_k & (x_k ^ term_2);
        }
        row[M >> 6]&= (1llu << (M & 63)) - 1;
    }
    int ret = 0;
    for(int j=0; j<M; j++) if(GET(row, j)) ret++;
    return ret;
}

int main(){OJize();
    string A, B; cin>>A>>B;
    int ans = 0;
    for(int i=0; i<B.size(); i++){
        rotate(B.begin(), B.begin()+1, B.end());
        ans = max(ans, lcs(A, B));
    }
    reverse(entire(B));
    for(int i=0; i<B.size(); i++){
        rotate(B.begin(), B.begin()+1, B.end());
        ans = max(ans, lcs(A, B));
    }
    cout<<ans;
}

Compilation message

rowords.cpp: In function 'int main()':
rowords.cpp:41:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<B.size(); i++){
                  ~^~~~~~~~~
rowords.cpp:46:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<B.size(); i++){
                  ~^~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 2 ms 508 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 4 ms 376 KB Output is correct
6 Correct 7 ms 376 KB Output is correct
7 Correct 138 ms 444 KB Output is correct
8 Correct 134 ms 248 KB Output is correct
9 Correct 136 ms 432 KB Output is correct
10 Correct 135 ms 504 KB Output is correct
11 Correct 146 ms 376 KB Output is correct
12 Correct 174 ms 376 KB Output is correct
13 Correct 172 ms 376 KB Output is correct
14 Correct 149 ms 436 KB Output is correct
15 Correct 195 ms 376 KB Output is correct
16 Correct 146 ms 504 KB Output is correct
17 Correct 66 ms 376 KB Output is correct
18 Correct 141 ms 468 KB Output is correct
19 Correct 136 ms 548 KB Output is correct
20 Correct 131 ms 504 KB Output is correct
21 Correct 12 ms 376 KB Output is correct
22 Correct 22 ms 380 KB Output is correct
23 Correct 37 ms 376 KB Output is correct
24 Correct 38 ms 376 KB Output is correct
25 Correct 59 ms 376 KB Output is correct