Obfuscation_HDU - 2340

题意分析:

题目中的加密字符串有其固定的组成规则:首尾字母不变,中间的字母顺序被打乱,然后去掉空格组成。所以,解译这段字符串的思路就是:匹配首字母相同的, 再去匹配尾字母相同的,最后再去比较中间字符是否相同。

其中需要注意两点:

第一点:当对首尾字母之间的字母排序时, 特别注意当 len < 4 的时候, 否则会WA。

第二点:当在中间某位置又多种选择可以达到时, 要将其标记下来,并将此状态维护到该种情况下所能达到的最后步,最后只需查看最后一步的状态即可。


Description

It is a well-known fact that if you mix up the letters of a word, while leaving the first and last letters in their places, words still remain readable. For example, the sentence “tihs snetncee mkaes prfecet sesne”, makes perfect sense to most people.

If you remove all spaces from a sentence, it still remains perfectly readable, see for example: “thissentencemakesperfectsense”, however if you combine these two things, first shuffling, then removing spaces, things get hard. The following sentence is harder to decipher: “tihssnetnceemkaesprfecetsesne”.

You’re given a sentence in the last form, together with a dictionary of valid words and are asked to decipher the text.

Input

On the first line one positive number: the number of testcases, at most 100. After that per testcase:

One line with a string s: the sentence to decipher. The sentence consists of lowercase letters and has a length of at least 1 and at most 1 000 characters.

One line with an integer n with 1 ≤ n ≤ 10 000: the number of words in the dictionary.

n lines with one word each. A word consists of lowercase letters and has a length of at least 1 and at most 100 characters. All the words are unique.

Output

Per testcase:

One line with the deciphered sentence, if it is possible to uniquely decipher it. Otherwise “impossible” or “ambiguous”, depending on which is the case.

Sample Input

3tihssnetnceemkaesprfecetsesne

5

makes

perfect

sense

sentence

this

hitehre

2

there

hello

hitehre

3

hi

there

three

Sample Output

this sentence makes perfect sense

impossible

ambiguous


ps:这一份是用bfs写的, 也可以用dfs写(虽然我的一直T,没改出来T...T),,,

上代码:

#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
using namespace std;

const int maxn = 10000 + 10;

int n, len, ok, repeat, vis[maxn];
string text;
char dic[maxn][120];
struct node {
    int len, id;
    char rear;
    string sorts;
};
vector <node> words[30];

void bfs() {
    vector <int> ans[1005];
    memset(vis, 0, sizeof vis);
    vis[0] = 1;
    queue <int> q;
    q.push(0);
    while (q.size()) {
        int pos = q.front(); q.pop();
        if (pos >= len) continue;
        int t = text[pos] - 'a';
        for (int i = 0; i < words[t].size(); i++) {
                node u = words[t][i];
                int next_pos = pos + u.len;
                if (next_pos > len || text[next_pos - 1] != u.rear) continue;
                string temp = text.substr(pos, u.len);
                if (temp.length() > 3) sort(temp.begin() + 1, temp.end() - 1);
                if (temp == u.sorts) {

                    if (!vis[next_pos]) {
                        if (vis[pos] == 1) {
                            
                            ans[next_pos] = ans[pos];
                            ans[next_pos].push_back(u.id);
                        }
                        else vis[next_pos] += 2;
                        q.push(next_pos);
                        
                    }
                    vis[next_pos] ++;
                    

                }
            }
    }

    if (!vis[len]) printf("impossible\n");
    else if (vis[len] > 1) printf("ambiguous\n");
    else {
        for (int i = 0; i < ans[len].size(); i++) {
            printf("%s", dic[ans[len][i]]);
            if (i < ans[len].size() - 1) printf(" ");
            else printf("\n");
        }
    }
}

int main() {

    int T;
    scanf("%d", &T); getchar();
    while (T--) {
        scanf("%s", dic[0]);
        text = dic[0];
        len = text.length();
        for (int i = 0; i < 30; i++) words[i].clear();
        scanf("%d", &n); getchar();
        node temp;
        for (int i = 1; i <= n; i++) {
            scanf("%s", dic[i]);
            temp.sorts = dic[i];
            temp.len = temp.sorts.length();
            temp.rear = temp.sorts[temp.len - 1];
            temp.id = i;
            if (temp.len > 3) sort(temp.sorts.begin() + 1, temp.sorts.end() - 1);
            words[temp.sorts[0] - 'a'].push_back(temp);
        }
        bfs();
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值