我交上去TLE了,90分,而且T的那个测试点是极端的 1.00s TLE,提交记录。现在是吸了氧才过的,想知道还有没有卡常的方法使得不吸氧也能过。
#include <bits/stdc++.h>
using namespace std;
#define reg register
namespace Main
{
typedef long long ll;
const int maxn=3005;
const ll b=51971;
int n;
char s[maxn];
char t[maxn];
ll ans[maxn*maxn];
int tot;
void main()
{
scanf("%d",&n);
scanf("%s",s+1);
scanf("%s",t+1);
for(reg int i=1;i<=n;i++)
{
reg int pos=1;//找子序列的指针
reg ll val=0;//哈希值
for(reg int j=i;j<=n;j++)
{
while(pos<=n&&(s[pos]!=t[j]))pos++;
if(pos>n)
{//说明找不到与(t串当前序列)相同的(s串的子序列)了
break;
}
pos++;//跳到下一个字符,同时防止误判
val=(val*b+ll(t[j]-'a'+1));
ans[++tot]=val;
}
}
sort(ans+1,ans+tot+1);
reg int out=0;
for(reg int i=1;i<=tot;i++)
{
if(ans[i]!=ans[i-1])
{
out++;
}
}
printf("%d",out);
}
}
int main()
{
Main::main();
#ifndef ONLINE_JUDGE
system("pause");
#endif
return 0;
}