键盘读入主串S和模式串T,用BF(或KMP)算法判断模式串T是否为主串S的子串;用凯撒密码对模式串T加密,然后比较主串S和加密后的模式串T的大小
时间: 2024-05-01 07:22:08 浏览: 16
以下是使用BF算法判断模式串T是否为主串S的子串的代码:
```python
def isSubstring(S, T):
n = len(S)
m = len(T)
for i in range(n-m+1):
j = 0
while j < m and S[i+j] == T[j]:
j += 1
if j == m:
return True
return False
```
其中,n和m分别为主串S和模式串T的长度。BF算法的思想是,从主串的第一个字符开始,依次与模式串的第一个字符、第二个字符……逐个比较,如果全部相同,则说明模式串是主串的子串。
以下是对模式串T进行凯撒加密的代码:
```python
def caesarCipher(T, shift):
result = ""
for c in T:
if c.isalpha():
if c.isupper():
result += chr((ord(c) + shift - 65) % 26 + 65)
else:
result += chr((ord(c) + shift - 97) % 26 + 97)
else:
result += c
return result
```
其中,shift为凯撒密码的位移量。对于每个字母,根据它是大写字母还是小写字母进行不同的加密。如果不是字母,则原样输出。
最后,可以将加密后的模式串T和主串S进行大小比较,例如:
```python
S = "hello world"
T = "world"
shift = 3
if isSubstring(S, T):
encrypted_T = caesarCipher(T, shift)
if S > encrypted_T:
print(T, "is a substring of", S, "and the encrypted T is smaller")
else:
print(T, "is a substring of", S, "and the encrypted T is larger")
else:
print(T, "is not a substring of", S)
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)