Cyclic Shift(字符串匹配水题)

探讨在给定两个相同长度的字符串情况下,通过一次循环移位子序列使两字符串相等的可能性。代码实现使用C++,遍历并比较字符,判断是否能通过特定操作达到目标。

You are given two strings a and b of the same length and consisting of lowercase English letters. You can pick at most one subsequence of string b and do a cyclic shift on that subsequence exactly once.

For example, if you have a string “abcdefg” and you picked the letters at indices 2, 5, and 6 as a subsequence to do a cyclic shift on them, the letter at index 2 will go to index 5, the letter at index 5 will go to index 6, the letter at index 6 will go to index 2, and the string will become “afcdbeg”.

Your task is to check if it is possible to make string b equivalent to string a using at most one cyclic shift. Can you?
Input

The first line contains an integer T (1 ≤ T ≤ 200) specifying the number of test cases.

The first line of each test case contains an integer n (1 ≤ n ≤ 105) specifying the length of strings a and b. Then two lines follow, giving strings a and b, respectively. Both strings consist only of lowercase English letters.
Output

For each test case, print a single line containing “YES” (without quotes) if it is possible to make string b equivalent to string a using at most one cyclic shift. Otherwise, print “NO” (without quotes).
Example
Input

2
6
abcdef
adcebf
4
abcd
dabd

Output

YES
NO

#include<cstdio>
#include<iostream>
#include<cstring> 
using namespace std;
const int maxn=1e5+5;
char a[maxn],b[maxn],aa[maxn],bb[maxn];
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		int n;
		scanf("%d",&n);
		scanf("%s",a+1);
		scanf("%s",b+1);
		int t=0;
		for(int i=1;i<=n;i++){
			if(a[i]!=b[i]){
				aa[t]=a[i];
				bb[t]=b[i];
				t++;
			}
		}
		bool flag=true;
		for(int i=0;i<t;i++){
			if(bb[i]==aa[(i+1)%t])
			continue;
			else{
				flag=false;
				break;
			}
		}
		if(flag) printf("YES\n");
		else printf("NO\n");
	}
	return 0;
}


### 将字符串转换为系统循环码的方法 在讨论将字符串转换为系统循环码之前,有必要澄清什么是系统循环码。通常情况下,“系统循环码”这一术语并不常见于标准编程或数据处理领域内[^1]。然而,在特定上下文中,这可能指代某种自定义编码方案或是特定应用中的概念。 假设这里所提到的“系统循环码”是指通过某种算法对原始字符串进行变换得到的新形式,则具体实现取决于该编码规则的设计原则。一种常见的做法是对字符集内的每一个字符按照预定规律移动位置来形成新的表示方式。 对于Java语言环境下的操作,如果目标是基于位移或其他简单映射关系来进行编码转换,那么可以利用`StringBuilder`类配合基本逻辑运算完成此过程: ```java public class SystemCyclicCode { public static String convertToSystemCyclic(String input, int shift) { StringBuilder result = new StringBuilder(); for (char c : input.toCharArray()) { if (Character.isLetter(c)) { // Only apply to letters char base = Character.isUpperCase(c) ? 'A' : 'a'; result.append((char)(((c - base + shift) % 26) + base)); } else { result.append(c); // Non-letter characters remain unchanged } } return result.toString(); } public static void main(String[] args) { String originalText = "HelloWorld"; int cyclicShift = 3; String encodedText = convertToSystemCyclic(originalText, cyclicShift); System.out.println("Original Text: " + originalText); System.out.println("Encoded Text with Cyclic Shift of " + cyclicShift + ": " + encodedText); } } ``` 上述代码展示了如何创建一个简易版本的字母表偏移函数,它会根据给定的位移量改变输入字符串中每个英文字母的位置,而非英文字符保持不变。请注意实际应用场景下所需的具体编码规则可能会更加复杂,并且应当依据项目需求定制化开发相应的解决方案[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值