数据结构-字符串(KMP)循环旋转

要求:

请设计一个线性时间的算法,判断字符串 S 是否是另一个字符串 S’ 的循环旋转。例如, arc 和 car 是彼此的循环旋转。

分析:

S与S’如果长度不等,不满足循环旋转条件

解决这道题的关键应该是如果S + S得到的T字符串中能找到S’的子串那么就可以得出,S和S’是循环旋转的,所以这道题可以用kmp算法来解。

如果字符串S是字符串S'的循环旋转,那么将字符串S重复一次得到的字符串一定是字符串S'的子串。  具体来说,我们可以将字符串S复制一遍,得到字符串T=S+S。然后我们可以使用KMP算法在字符串T中查找字符串S'。如果字符串S'在T中出现了,那么字符串S一定是字符串S'的循环旋转。


两种模板解题:

教材版:

#include<iostream>
#include <algorithm>
#include <string>
using namespace std;

//计算字符串特征向量(优化版)
int* findNext(string P) {
	int i, k;
	int m = P.length();					// m为模式P的长度
	int* ne = new int[m];				// 动态存储区开辟整数数组
	ne[0] = -1;
	i = 0; k = -1;
	while (i < m - 1) {					// 若写成 i < m 会越界
		while (k >= 0 && P[k] != P[i])	// 采用 KMP 找最大首尾子串
			k = ne[k];				// k 递归地向前找
		i++;
		k++;
		if (P[k] == P[i])
			ne[i] = ne[k];			// 前面找 k 值,没有受优化的影响
		else
			ne[i] = k;				// 取消if判断,则不优化
	}
	return ne;
}

//KMP匹配
int KMPStrMatching(string T, string P, int* N) {
	int i = 0;
	int j = 0;
	int tLen = T.length(); // 目标的长度
	int pLen = P.length(); // 模式的长度
	if (tLen < pLen) // 若目标比模式短,匹配无法成功
		return -1;
	while (i < tLen && j < pLen) { // 反复比较,进行匹配
		if (j == -1 || T[i] == P[j])
			i++, j++;
		else j = N[j];	// 不相等,按照特征向量调整
	}
	if (j >= pLen)
		return (i - pLen); // 注意仔细算下标
	else
		return -1;
}

int main() {
	string s1, s2;
	cin >> s1 >> s2;
	if (s1.length() != s2.length()) {
		cout << "No match";
		return 0;
	}
	s1 = s1 + s1;
	int* ne = new int[100];
	next = findNext(s2);
	if (KMPStrMatching(s1, s2, next) == -1)
		cout << "No match";
	else
		cout << "match successfully";
}

数组模拟:

/*
 * ......................................&&.........................
 * ....................................&&&..........................
 * .................................&&&&............................
 * ...............................&&&&..............................
 * .............................&&&&&&..............................
 * ...........................&&&&&&....&&&..&&&&&&&&&&&&&&&........
 * ..................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&..............
 * ................&...&&&&&&&&&&&&&&&&&&&&&&&&&&&&.................
 * .......................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.........
 * ...................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...............
 * ..................&&&   &&&&&&&&&&&&&&&&&&&&&&&&&&&&&............
 * ...............&&&&&@  &&&&&&&&&&..&&&&&&&&&&&&&&&&&&&...........
 * ..............&&&&&&&&&&&&&&&.&&....&&&&&&&&&&&&&..&&&&&.........
 * ..........&&&&&&&&&&&&&&&&&&...&.....&&&&&&&&&&&&&...&&&&........
 * ........&&&&&&&&&&&&&&&&&&&.........&&&&&&&&&&&&&&&....&&&.......
 * .......&&&&&&&&.....................&&&&&&&&&&&&&&&&.....&&......
 * ........&&&&&.....................&&&&&&&&&&&&&&&&&&.............
 * ..........&...................&&&&&&&&&&&&&&&&&&&&&&&............
 * ................&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&............
 * ..................&&&&&&&&&&&&&&&&&&&&&&&&&&&&..&&&&&............
 * ..............&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&....&&&&&............
 * ...........&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&......&&&&............
 * .........&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&.........&&&&............
 * .......&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...........&&&&............
 * ......&&&&&&&&&&&&&&&&&&&...&&&&&&...............&&&.............
 * .....&&&&&&&&&&&&&&&&............................&&..............
 * ....&&&&&&&&&&&&&&&.................&&...........................
 * ...&&&&&&&&&&&&&&&.....................&&&&......................
 * ...&&&&&&&&&&.&&&........................&&&&&...................
 * ..&&&&&&&&&&&..&&..........................&&&&&&&...............
 * ..&&&&&&&&&&&&...&............&&&.....&&&&...&&&&&&&.............
 * ..&&&&&&&&&&&&&.................&&&.....&&&&&&&&&&&&&&...........
 * ..&&&&&&&&&&&&&&&&..............&&&&&&&&&&&&&&&&&&&&&&&&.........
 * ..&&.&&&&&&&&&&&&&&&&&.........&&&&&&&&&&&&&&&&&&&&&&&&&&&.......
 * ...&&..&&&&&&&&&&&&.........&&&&&&&&&&&&&&&&...&&&&&&&&&&&&......
 * ....&..&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&...........&&&&&&&&.....
 * .......&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&..............&&&&&&&....
 * .......&&&&&.&&&&&&&&&&&&&&&&&&..&&&&&&&&...&..........&&&&&&....
 * ........&&&.....&&&&&&&&&&&&&.....&&&&&&&&&&...........&..&&&&...
 * .......&&&........&&&.&&&&&&&&&.....&&&&&.................&&&&...
 * .......&&&...............&&&&&&&.......&&&&&&&&............&&&...
 * ........&&...................&&&&&&.........................&&&..
 * .........&.....................&&&&........................&&....
 * ...............................&&&.......................&&......
 * ................................&&......................&&.......
 * .................................&&..............................
 * ..................................&..............................
 */


/*
 * @Description: 
 * @Author: YOLOKY
 * @Date: 2023-03-25 18:15:10
 * @LastEditors: YOLOKY
 * @LastEditTime: 2023-03-25 18:36:38
 */
#include <iostream>
#include <string>
using namespace std;

const int N = 1000010;

int ne[N];

int main()
{
    string s, p;
    cin >> s >> p;

    if (s.length() != p.length()) {
        cout << "No match" << endl;
        system("pause");
        return 0;
    }

    string st = s + s;
    int n = st.length(), m = p.length();

    // 构造next数组
    for (int i = 2, j = 0; i <= m; i++) {
        while (j && p[i - 1] != p[j]) j = ne[j];
        if (p[i - 1] == p[j]) j++;
        ne[i] = j;
    }

    // 在st中匹配t
    for (int i = 1, j = 0; i <= n; i++) {
        while (j && st[i - 1] != p[j]) j = ne[j];
        if (st[i - 1] == p[j]) j++;
        if (j == m) {
            cout << "match successfully" << endl;
            system("pause");
            return 0;
        }
    }

    cout << "No match" << endl;
    system("pause");
    
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_62651190/article/details/129770536