大佬们谁能帮我优化一下(刚学两月的蒟蒻)
查看原帖
大佬们谁能帮我优化一下(刚学两月的蒟蒻)
544029
yeunmer_233楼主2021/10/17 13:46

295ms比较慢(没有优化)

下面是代码(大佬帮忙看看有什么多余的)

orz谢谢~~
//*** *** *** *** ***
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
using namespace std;
vector<int>v;
int s[1005][105]={};
bool check(int k,int st,int l)
{
	for(int i=0;i<l;i++)
	{
		if(s[k][st+i]!=v[i])
		{
			return 0;
		}
	}
	return 1;
}
int main()
{
	int n;
	scanf("%d",&n);
	int minl=1e8+2;
	int x,flag;
	int now,last;
	for(int i=1;i<=n;i++)
	{
		now=0;last=0;
		scanf("%d",&x);
		if(x<minl)
		{
			minl=x;
			flag=i;	
		} 
		for(int k=0;k<x;k++)
		{
			scanf("%d",&now);
			s[i][k]=now-last;
			last=now;
		}
	}
	//
	int ans_maxl=1;
	for(int i=1;i<=minl;i++)
	{
		bool con_next=1;
		for(int l=1;l<=minl-i;l++)
		{
			if(con_next)
			{
				v.push_back(s[flag][i+l-1]);
				bool pd=1;
				for(int k=1;k<=n;k++)
				{
					if(pd==1)
					{
						bool pdd=0;
						for(int st=1;st<=101-l;st++)
						{
							if(pdd==1)
							{
								continue;
							}
							pdd=check(k,st,l);
						}
						if(pdd==0) pd=0;
					}
				}
				if(pd==1)
				{
					ans_maxl=max(ans_maxl,1+l);
				}
				else
				{
					con_next=0;
					while(!v.empty())
					{
						v.pop_back();
					}
				}
			}
		}
	}
	cout<<ans_maxl;
	return 0;
}
2021/10/17 13:46
加载中...