第一题: HDU 4551 生日猜猜猜
直接暴力求解,枚举每个月每一天。
只是开始的时候没有注意到可能有多种符合,我是直接break的,WA了。
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
using namespace std;
const int N=1510;
typedef long long LL;
bool run(int y)
{
return (y%4==0&&y%100!=0)||y%400==0;
}
int gcd(int m,int n)//最大公约数
{
int t;
while(n)
{ t=m%n; m=n; n=t; }
return m;
}
int main()
{
int n,T,m,x,z,y,d,aa,bb;
int i,j;
int ti=0;
cin>>T;
while(T--)
{
int a[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
cin>>x>>y>>z;
if(run(z))
a[2]++;
int flag=0;
for(i=1;i<=12;i++)
for(j=1;j<=a[i];j++)
{
int q=gcd(i,j);
int p=i*j/q;
if(q==x&&p==y)
{
flag++;
aa=i;
bb=j;
}
}
if(flag==0)
printf("Case #%d: -1\n",++ti);
else if(flag>1)
printf("Case #%d: 1\n",++ti);
else
printf("Case #%d: %d/%02d/%02d\n",++ti,z,aa,bb);
}
return 0;
}
第二题: HDU 4552 怪盗基德的挑战书
kmp算法,用后缀数组也行。
#include <iostream>
#include <queue>
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
using namespace std;
const int N=100005;
typedef __int64 LL;
char str[N];
int c[N];
int next[N];
int ans;
int n;
void getNext(char *p,int *next)
{
int j,k;
next[0]=-1;
j=0;
k=-1;
while(j<n)
{
if(k==-1||p[j]==p[k])
{
if(k!=-1)
{
c[j]=c[k]+1;
ans+=c[j];
}
j++;
k++;
next[j]=k;
}
else
k=next[k];
}
}
int main ()
{
while(scanf("%s",str)!=EOF)
{
ans=0;
n=strlen(str);
memset(c,0,sizeof(c));
getNext(str,next);
ans=(ans+n)%256;
printf("%d\n",ans);
}
}
待续。。。。