这种思路正确吗..?
查看原帖
这种思路正确吗..?
444063
Jessica2333楼主2023/7/16 20:13

用两个栈,一个一位一位的存S,一个存当前的S[i]能匹配成功的最大T前缀的长度;当匹配成功的最大T前缀的长度为T的长度时,两个栈都pop掉T的这部分,然后再把T的第二个栈栈顶+1位和S[i]继续比较...最终答案从栈底输出第一个栈(手写的栈)。代码没调出来WA了QAQ

有大佬帮忙看看思路对吗吗?

#include<iostream>
using namespace std;
string S,T;
char sta[1000008];
int top=0,len[1000008],top2=0;
int main()
{
	int i,j,k,tmp;
	cin>>S>>T;
	S=" "+S;
	T=" "+T; 
	for(i=1,j=1;i<S.length();i++,j++)
	{
		sta[++top]=S[i];
		if(S[i]==T[j])
		{
			tmp=len[top2];
			tmp++;
			len[++top2]=tmp;
		}
		else
		{
			if(S[i]==T[1]) len[++top2]=1,j=1;
			else len[++top2]=0,j=0;
		}
		if(j==T.length()-1)
		{
			top-=j;
			top2-=j;
			j=len[top2];
		}
	}
	for(i=1;i<=top;i++)
		cout<<sta[i];
	return 0;
}
/*
aabaaabacc
bac
*/
2023/7/16 20:13
加载中...