我觉得我比赛的时候一定是脑抽了,木有观察乘法运算的规律,竟然写了个DFS然后四分钟内跑不完就弃疗了。果真没有队友那种“这道题是肯定要过的”魄力。
首先如果一个数列可以化成ijk的话那么这个数列的乘积一定是-1。
因为一个L长度的子串的积一定是+-1,+-i,+-j,+-k,所以四个字串的积一定是1。所以在计算数列的积的时候只要考虑最后X%4的部分。要注意这里面的乘法是两个block相乘,第二个乘数不一定是正数,所以对sgn的处理和之前不一样。例如-i*-i。这一块还是分类讨论比较好。我之前错了好久。><
因为4*L子串的积一定是1,所以从前往后找最靠前的乘积为i的子串,一定可以在4*L之内找到。否则前面都是因子1没有意义。同理,从后往前找最靠后的乘积为k的子串,也可以在4*L之内找到。如果i,k都存在并且k在i后面,那么中间一定是j。其实也可以找到i之后直接往后找j。
注意large input要把X和index of i and k换成long long。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<ctype.h>
#include<map>
#include<time.h>
#include<bitset>
#include<set>
#include<list>
using namespace std;
//gcj quali Pro C
const int maxn=10010;
int T;
long long L;
long long X;//注意large input要换成long long防止溢出
int str[maxn];
int mp[5][5]={{0,0,0,0,0},{0,1,2,3,4},{0,2,-1,4,-3},{0,3,-4,-1,2},{0,4,3,-2,-1}};
int sgn=1;
int pro=1;
bool flg1=false;
bool flgi=false;
long long loci;
void calc(int st,int ed)
{
pro=1;
sgn=1;
for(int i=st;i<ed;i++)
{
pro=mp[pro][str[i]];
if(pro<0)
{
sgn=-sgn;
pro=abs(pro);
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
freopen("C-large-practice.in","r",stdin);
freopen("out1.txt","w",stdout);
scanf("%d",&T);
for(int ca=1;ca<=T;ca++)
{
pro=1;
sgn=1;
flg1=false;
flgi=false;
loci=0;
memset(str,0,sizeof(str));
scanf("%lld %lld",&L,&X);
char ss[maxn];
scanf("%s",&ss);
for(int i=0;i<L;i++)
{
str[i]=ss[i]-'i'+2;
}
sgn=1;
if(L*X<3)
{
printf("Case #%d: NO\n",ca);
continue;
}
else
{
calc(0,L);
//cout<<pro<<" "<<sgn<<endl;
int tmpx=X%4;
if(tmpx==0)
{
flg1=false;
}
else if(tmpx==1)
{
if(pro==1&&sgn==-1)
{
flg1=true;
}
}
else if(tmpx==2)
{
pro=mp[pro][pro];
if(pro<0)
{
sgn=-1;
pro=-pro;
}
if(pro==1&&sgn==-1)
{
flg1=true;
}
}
else if(tmpx==3)
{
if(pro==1&&sgn==-1)
{
flg1=true;
}
}
// else
// {
// for(int i=1;i<tmpx;i++)
// {
// pro=mp[pro][pro];//这里面是两个block相乘,第二个乘数不一定是sgn=1,例如tmpx=2时的-i*-i
// if(pro<0)
// {
// sgn=-sgn;
// pro=-pro;
// }
// }
// if(pro==1&&sgn==-1)
// {
// flg1=true;
// }
// }
// }
}
if(flg1==false)
{
printf("Case #%d: NO\n",ca);
continue;
}
pro=1;
sgn=1;
for(long long i=0;i<4&&i<X;i++)
{
for(int j=0;j<L;j++)
{
pro=mp[pro][str[j]];
if(pro<0)
{
pro=-pro;
sgn=-sgn;
}
if(pro==2&&sgn==1)
{
flgi=true;
loci=i*L+j;
break;
}
}
}
if(flgi==false)
{
printf("Case #%d: NO\n",ca);
continue;
}
pro=1;
sgn=1;
for(int i=0;i<min((long long)4,X);i++)
{
for(int j=L-1;j>=0;j--)
{
pro=mp[str[j]][pro];
if(pro<0)
{
pro=-pro;
sgn=-sgn;
}
if(pro==4&&sgn==1)
{
long long lock=L*X-i*L-(L-j);
if(lock>loci)
{
printf("Case #%d: YES\n",ca);
goto next;
}
}
}
}
printf("Case #%d: NO\n",ca);
next:
;
}
return 0;
}
顺便附上我的傻逼DFS代码==以后做题要动脑子额。
#include<iostream>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<ctype.h>
#include<map>
#include<time.h>
#include<bitset>
#include<set>
#include<list>
using namespace std;
//gcj quali Pro C
const int maxn=10010;
int T;
int L;
int X;
int str[maxn];
int mp[5][5]={{0,0,0,0,0},{0,1,2,3,4},{0,2,-1,4,-3},{0,3,-4,-1,2},{0,4,3,-2,-1}};
bool ans=0;
int sgn=1;
void dfs(int dep,int st,int aim,int sgn)
{
//cout<<"aim "<<aim<<endl;
if(aim>=5)
{
if(st==(L*X))
{
ans=true;
}
return;
}
int pro=1;
//sgn=1;
for(int i=st;i<L*X;i++)
{
if(mp[pro][str[i]]>0)
{
pro=mp[pro][str[i]];
}
else
{
sgn=-sgn;
pro=abs(mp[pro][str[i]]);
}
// cout<<pro<<" "<<sgn<<endl;
if(pro==aim&&sgn==1)
{
if(ans==false)
{
dfs(dep+1,i+1,aim+1,sgn);
}
}
}
}
int main()
{
//freopen("input.txt","r",stdin);
freopen("C-small-attempt1.in","r",stdin);
freopen("C-small-output.out","w",stdout);
scanf("%d",&T);
for(int ca=1;ca<=T;ca++)
{
scanf("%d %d",&L,&X);
char ss[maxn];
scanf("%s",&ss);
for(int i=0;i<X;i++)
{
for(int j=0;j<L;j++)
{
str[i*L+j]=ss[j]-'i'+2;
// cout<<str[i*L+j]<<" ";
}
}
sgn=1;
ans=0;
if(L*X<3)
{
ans=0;
}
else
{
dfs(0,0,2,1);
}
if(ans==0)
{
printf("Case #%d: NO\n",ca);
}
else if(ans==1)
{
printf("Case #%d: YES\n",ca);
}
}
return 0;
}