zoj 3817 Chinese Knot(hash+暴力),zoj3817

來源:互聯網
上載者:User

zoj 3817 Chinese Knot(hash+暴力),zoj3817

題目連結:zoj 3817 Chinese Knot

題目大意:給出四個字串,對應著同心結的四條邊,現在給定一個目標串,可以從任意節點開始移動,問是否可以匹配目標串。

解題思路:用hash將四個字串的正序和逆序處理出來,然後dfs枚舉,每次保留起始位置和移動方向即可。

#include <cstdio>#include <cstring>#include <vector>#include <algorithm>using namespace std;typedef unsigned long long ull;typedef pair<int, int> pii;#define lson(x) ((x)<<1)#define rson(x) (((x)<<1)|1)const int maxn = 100005;const ull X = 107;char str[maxn];int N, M;vector<pii> ans;ull G[12][maxn], H[maxn], T[maxn];void init () {    scanf("%d%d", &N, &M);    for (int i = 0; i < 4; i++) {        scanf("%s", str+1);        G[lson(i)][N+1] = G[rson(i)][N+1] = 0;        for (int j = N; j >= 1; j--) {            G[lson(i)][j] = G[lson(i)][j+1] * X + (str[j] - 'a');            G[rson(i)][j] = G[rson(i)][j+1] * X + (str[N-j+1] - 'a');        }    }    scanf("%s", str+1);    H[M+1] = 0;    for (int j = M; j >= 1; j--)        H[j] = H[j+1] * X + (str[j] - 'a');}bool dfs (int k, int pos, int dir, int L) {    if (L == 0)        return true;    int len = min(N - pos + 1, L);    if (G[(k<<1)|dir][pos] - G[(k<<1)|dir][pos+len] * T[len] == H[M-L+1] - H[M-L+len+1] * T[len]) {        ans.push_back(make_pair(k * N + (dir ? N + 1 - pos : pos), dir));        L -= len;        if (L == 0)            return true;        for (int i = 0; i < 4; i++) {            for (int d = 0; d < 2; d++) {                if (k == i && (d^1) == dir)                    continue;                if (dfs(i, 1, d, L))                    return true;            }        }        ans.pop_back();    }    return false;}bool solve () {    for (int i = 0; i < 4; i++) {        for (int pos = 1; pos <= N; pos++) {            for (int d = 0; d < 2; d++) {                ans.clear();                if (dfs(i, pos, d, M))                    return true;            }        }    }    return false;}int main () {    int cas;    scanf("%d", &cas);    T[0] = 1;    for (int i = 1; i < maxn; i++)        T[i] = T[i-1] * X;    while (cas--) {        init();        if (solve()) {            int p = 0;            for (int k = 0; k < ans.size(); k++) {                int u = (ans[k].first - 1) / N + 1, dir = ans[k].second;                if (dir == 0) {                    for (int i = ans[k].first; i <= u * N && p < M; i++, p++) {                        if (p) printf(" ");                        printf("%d", i);                    }                } else {                    for (int i = ans[k].first; i > (u-1) * N && p < M; i--, p++) {                        if (p) printf(" ");                        printf("%d", i);                    }                }            }            printf("\n");        } else            printf("No solution!\n");    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.