首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >UVA12604「Caesar Cipher」

UVA12604「Caesar Cipher」

作者头像
hotarugali
发布于 2022-03-02 12:24:06
发布于 2022-03-02 12:24:06
63700
代码可运行
举报
运行总次数:0
代码可运行

1. 题目

题目链接:UVA12604「Caesar Cipher」

Description

In cryptography, a Caesar cipher, also known as Caesar’s cipher, the shift cipher, Caesar’s code or Caesar shift, is one of the simplest and most widely known encryption techniques. It is a type of substitution cipher in which each letter in the plaintext is replaced by a letter some fixed number of positions down the alphabet (wrapping around in the end). For example, given an alphabet of capital letters in usual order, with a shift of 3, A would be replaced by D, B would become E, and so on, with Z being replaced by C. The method is named after Julius C Caesar, who used it in his private correspondence.

We are given an alphabet A, a string Swhich is encrypted using a shift cipher and a plaintext word W. Find the possible values of shifts (in the range [0,|A|1] )used in encryption if we know that the unencrypted text contains exactly one occurrence of the wordW.

Input

Input starts with an integer Non a line, the number of test cases. Each cases contains three strings on separate lines, alphabet A, plaintext wordW and encrypted text S. AlphabetA will contain only letters and digits (['A'-'Z']['a'-'z']['0'-'9']) and its symbol order is not necessarily lexicographical (see the third sample case). Awill not contain duplicate symbols. The constraints are as given: 3|A|62;1|W|50,000;3|S|500,000.

Output

For each test case print one line of output.

  • If there are no shifts that would satisfy the condition of W being a part of the unencryptedS, print no solution.
  • If there is exactly one shift that could have been used, print unique: # where # is the shift value.
  • It there are more than one possible shifts print ambiguous: followed by the sorted list of shift values.

For clarification, see the sample output.

Sample Input

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
4
ABC
ABC
ABCBBBABC
ABC
ABC
ABCBCAABC
D7a
D7a
D7aaD77aDD7a
ABC
ABC
ABC

Sample Output

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
no solution
unique: 1
ambiguous: 1 2
unique: 0

2. 题解

分析

显然是一道 KMP 题,由于|A|较小,故考虑直接枚举W 加密后所有可能的密文,即枚举shift=0..|A|1 ,然后对每个 W的密文应用 KMP 算法,查找其在主串 S中匹配的次数,只有次数为 1 时表示此 shift 有效,然后统计所有有效的 shift即可。

代码

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
#include <bits/stdc++.h>
using namespace std;

// KMP 算法
struct KMP {
    #ifndef _KMP_
    #define ll int 
    #define MAXN 500005
    #endif
    ll next[MAXN];
    KMP() {}
    // 计算失配数组 next
    void getNext(char *str, ll n) {
        next[0] = -1;
        ll i = 0, j = -1;
        while(i < n) {
            if(!(~j) || str[i] == str[j]) {
                ++i, ++j;
                if(str[i] != str[j])    next[i] = j;
                else    next[i] = next[j];
            } else {
                j = next[j];
            }
        }
    }
    // 计算主串中模式串的个数
    ll match(char *main, ll n, char *pattern, ll m) {
        ll ans = 0;
        ll i = 0, j = 0;
        while(i < n) {
            if(!(~j) || main[i] == pattern[j]) {
                ++i, ++j;
                if(j == m) {
                    ++ans;
                    j = next[j];
                }
            } else {
                j = next[j];
            }
        }
        return ans;
    }
};

int main()
{
    ll t;
    KMP kmp;
    char set[MAXN], word[MAXN], text[MAXN], sword[MAXN];
    ll pos[128];
    scanf("%d", &t);
    for(ll i = 0; i < t; ++i) {
        scanf("%s%s%s", set, word, text);
        ll s = strlen(set);
        ll m = strlen(word);
        ll n = strlen(text);
        for(ll j = 0; j < s; ++j) {
            pos[set[j]] = j;
        }
        vector <ll> ans;
        for(ll j = 0; j < s; ++j) {
            for(ll k = 0; k < m; ++k) {
                sword[k] = set[(pos[word[k]]+j)%s];
            }
            kmp.getNext(sword, m);
            if(kmp.match(text, n, sword, m) == 1) {
                ans.push_back(j);
            }
        }
        if(ans.empty()) {
            printf("no solution\n");
        } else if(ans.size() == 1) {
            printf("unique: %d\n", ans[0]);
        } else {
            printf("ambiguous:");
            for(ll i = 0; i < ans.size(); ++i) {
                printf(" %d", ans[i]);
            }
            printf("\n");
        }
    }
    return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020-09-04,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
字符串-KMP
KMP 原理 给出一篇大牛的详解参考,小伙伴们可以刨根究底:从头到尾彻底理解KMP 模板 void getnext(char* p, int lp) { nex[0] = nex[1] = 0;
唔仄lo咚锵
2020/10/09
3240
字符串-KMP
POJ 3461 kmp
Oulipo Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40168 Accepted: 16135 Description The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e'. He was a member of the Oulipo group. A
attack
2018/04/13
5680
【CodeForces】716B - Complete the Word(暴力)
ZS the Coder loves to read the dictionary. He thinks that a word is nice if there exists a substring (contiguous segment of letters) of it of length 26 where each letter of English alphabet appears exactly once. In particular, if the string has length strictly less than 26, no such substring exists and thus it is not nice.
FishWang
2025/08/26
690
HDUOJ-------The Hardest Problem Ever
The Hardest Problem Ever Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13035    Accepted Submission(s): 5905 Problem Description Julius Caesar lived in a time of danger and intrigue. The hardest
Gxjun
2018/03/21
7520
hdu------(4300)Clairewd’s message(kmp)
Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3433    Accepted Submission(s): 1334 Problem Description Clairewd is a member of FBI. After several years concealing in BUPT, sh
Gxjun
2018/03/26
6330
UVA12467「Secret Word」
Alicia and Roberto like to play games. Today, Roberto is trying to guess a secret word that Alicia chose. Alicia wrote a long string in a piece of paper and gave Roberto the following clues:
hotarugali
2022/03/02
7860
UVA Hangman Judge
英语太烂啊。 In ``Hangman Judge,'' you are to write a program that judges a series of Hangman games. For each game, the answer to the puzzle is given as well as the guesses. Rules are the same as the classic game of hangman, and are given as follows: The contest
用户1624346
2018/04/11
7360
2020 Multi-University Training Contest 8
给定长度为 n 的数组 a ,对于任意 i 都有 -1\leq a_i\leq 1 。想要将 a 划分做若干块使得每块总和的 sgn 和最大,每块的长度需要满足 l\leq len\leq r 。
wenzhuan
2022/08/15
2560
uva 11732 – strcmp() Anyone? 不错的Trie题
题解:http://blog.csdn.net/u013480600/article/details/23122503
全栈程序员站长
2022/07/13
1810
String Problem(KMP+最小表示法)- HDU 3374
Give you a string with length N, you can generate N strings by left shifts. For example let consider the string “SKYLONG”, we can generate seven strings: String Rank SKYLONG 1 KYLONGS 2 YLONGSK 3 LONGSKY 4 ONGSKYL 5 NGSKYLO 6 GSKYLON 7 and lexicographically first of them is GSKYLON, lexicographically last is YLONGSK, both of them appear only once. Your task is easy, calculate the lexicographically fisrt string’s Rank (if there are multiple answers, choose the smallest one), its times, lexicographically last string’s Rank (if there are multiple answers, choose the smallest one), and its times also.
ACM算法日常
2018/08/07
5550
字符串--Kmp详解+代码
        给定文本串text和模式串pattern,要求从文本串中找到模式串第一次出现的位置。
用户2965768
2018/08/30
8430
字符串--Kmp详解+代码
UVA455「Periodic Strings」
A character string is said to have period k if it can be formed by concatenating one or more repetitions of another string of length k. For example, the string abcabcabcabc has period , since it is formed by repetitions of the string abc. It also has periods 666 (two repetitions of abcabc) and (one repetition of abcabcabcabc).
hotarugali
2022/03/02
4580
HDOJ 1048 The Hardest Problem Ever(加密解密类)
Problem Description Julius Caesar lived in a time of danger and intrigue. The hardest situation Caesar ever faced was keeping himself alive. In order for him to survive, he decided to create one of the first ciphers. This cipher was so incredibly sound, that no one could figure it out without knowing how it worked. You are a sub captain of Caesar’s army. It is your job to decipher the messages sent by Caesar and provide to your general. The code is simple. For each letter in a plaintext message, you shift it five places to the right to create the secure message (i.e., if the letter is ‘A’, the cipher text would be ‘F’). Since you are creating plain text out of Caesar’s messages, you will do the opposite:
谙忆
2021/01/21
2960
字符串-AC自动机(详细图解)
Fail指针 同KMP的next一样,Fail指针是AC自动机的核心,是在树上指出失配后下一个跳转的位置,而不用全部回溯,大大减少时间。那么Fail是怎么跳转的? 以HDU-2222的样例为例说明,模式串P={“she”,“he”,“say”,“shr”,“her”},文本串S=“yasherhs”。
唔仄lo咚锵
2020/10/09
1.5K0
字符串-AC自动机(详细图解)
数据结构实验之串一:KMP简单应用 SDUT 2772
给定两个字符串string1和string2,判断string2是否为string1的子串。
Lokinli
2023/03/09
1770
uva 104 Bandwidth
  2)计算出当前序列中, 所有节点的dis[i], 并求出最大的dis[i] : max_dis
熙世
2019/07/15
4850
HDU 6629 (2019杭电第五场 1006) string matching (扩展kmp)
题意: 求字符串 s[i…len−1] and s[0…len−1] i>0 最长公共前缀长度求解过程的比较次数
用户2965768
2019/08/14
4100
HDU 1686 Oulipo(KMP)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1686 题意是输入一个模式串p,再输入一个文本串s,问p在s中出现了多少次。 KMP的裸
Ch_Zaqdt
2019/10/22
3450
BZOJ3670: [Noi2014]动物园(KMP)
给出一个字符串,定义$num[i]$表示在$[1, i]$区间内互不重复的相同前后缀的数量。
attack
2018/08/01
2890
BZOJ3670: [Noi2014]动物园(KMP)
PAT「1005 Programming Pattern (35分)」
题目链接:PAT「1005 Programming Pattern (35分)」 。
hotarugali
2022/03/01
2940
相关推荐
字符串-KMP
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档