Merge pull request #30 from KiritoTRw/master

Powered By HC TECH : AutoACer Engine
This commit is contained in:
Kirigaya Kazuto 2016-08-13 17:16:51 +08:00 committed by GitHub
commit dbb7f86160
97 changed files with 4536 additions and 0 deletions

35
HDOJ/1200_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<stdio.h>
#include<string.h>
int main()
{
char s[1000],a[100][100];
int i,j,k,l,m,n,t,f,g,len;
while(scanf("%d",&n)!=EOF&&n)
{ t=-1;
getchar();
gets(s);len=strlen(s);
for(i=0,j=-1;s[i]!='\0';i++)
{
if(i%n==0)
{ t++;j++;k=0;}
if(t%2==0)
{
a[j][k++]=s[i];
}
if(t%2==1)
{
f=i+n-1;
for(g=f;g>=i;g--)
a[j][k++]=s[g];
i=i+n-1;
}
}
for(i=0;i<n;i++)
{
for(j=0;j<=t;j++)
printf("%c",a[j][i]);
}
printf("\n");
}
return 0;
}

32
HDOJ/1201_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
int leap(int year)
{
if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
return 1;
}
return 0;
}
int main()
{
int T;
scanf("%d", &T);
int year, month, day;
while (T--) {
scanf("%d-%d-%d", &year, &month, &day);
int sum = 365 * 18;
for (int i = 1; i < 18; i++) {
sum += leap(i + year);
}
if (month <= 2) {
sum += leap(year);
} else {
sum += leap(year + 18);
}
if (month == 2 && day == 29) {
printf("-1\n");
} else {
printf("%d\n", sum);
}
}
return 0;
}

41
HDOJ/1202_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<iostream>
#include<iomanip>
int point(double );
using namespace std;
int main()
{
int n;
while(cin>>n)
{
double sum=0,s,p,c=0;
while(n--)
{
cin>>s>>p;
if(p==-1)
continue;
else
{
sum+=s;
c+=s*point(p);
}
}
if(c==0)
cout<<"-1\n";
else
cout<<setiosflags(ios::fixed)<<setprecision(2)<<c/sum<<endl;
}
return 0;
}
int point(double g)
{
if(g>=90)
return 4;
else if(g>=80)
return 3;
else if(g>=70)
return 2;
else if(g>=60)
return 1;
else
return 0;
}

29
HDOJ/1203_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a[10002];
double b[10002];
double p[10002];
int n,m;
void dp()
{
memset(p,0,sizeof(p));
for(int i=1;i<=m;++i)
{
for(int j=n;j>=a[i];--j)
p[j]=max(p[j],1-(1-p[j-a[i]])*(1-b[i]));
}
}
int main()
{
while(cin>>n>>m&&(m+n))
{
for(int i=1;i<=m;++i)
cin>>a[i]>>b[i];
dp();
printf("%.1f%%\n",p[n]*100);
}
return 0;
}

24
HDOJ/1204_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
int N,M;
double p,q,rate,k;
while(cin>>N>>M>>p>>q)
{
if(N==0){cout<<"0.00"<<endl;continue;}
if(M==0){cout<<"1.00"<<endl;continue;}
if(p==0||q==1){cout<<"0.00"<<endl;continue;}
if(q==0||p==1){cout<<"1.00"<<endl;continue;}
if(p==q) rate=1.0*N/(M+N);
else
{
k = q*(1-p)/(p*(1-q));
rate = (1.0-pow(k,N))/(1.0-pow(k,M+N));
}
cout<<fixed<<setprecision(2)<<rate<<endl;
}
return 0;
}

25
HDOJ/1205_autoAC.cpp Normal file
View File

@ -0,0 +1,25 @@
#include <stdio.h>
#define x 1000000
#define max(a,b) a>b?a:b
int i,n,T,Max;
int a[x];
int main()
{
scanf("%d",&T);
while(T--)
{
__int64 sum=0;
scanf("%d",&n);
Max=0;
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
Max=max(Max,a[i]);
}
sum=sum-Max+1;
if(sum>=Max) printf("Yes\n");
else printf("No\n");
}
return 0;
}

77
HDOJ/1207_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<iostream>
#include<cmath>
using namespace std;
int a[65];
int main()
{
a[1]=1;
a[2]=3;
a[3]=5;
a[4]=9;
a[5]=13;
a[6]=17;
a[7]=25;
a[8]=33;
a[9]=41;
a[10]=49;
a[11]=65;
a[12]=81;
a[13]=97;
a[14]=113;
a[15]=129;
a[16]=161;
a[17]=193;
a[18]=225;
a[19]=257;
a[20]=289;
a[21]=321;
a[22]=385;
a[23]=449;
a[24]=513;
a[25]=577;
a[26]=641;
a[27]=705;
a[28]=769;
a[29]=897;
a[30]=1025;
a[31]=1153;
a[32]=1281;
a[33]=1409;
a[34]=1537;
a[35]=1665;
a[36]=1793;
a[37]=2049;
a[38]=2305;
a[39]=2561;
a[40]=2817;
a[41]=3073;
a[42]=3329;
a[43]=3585;
a[44]=3841;
a[45]=4097;
a[46]=4609;
a[47]=5121;
a[48]=5633;
a[49]=6145;
a[50]=6657;
a[51]=7169;
a[52]=7681;
a[53]=8193;
a[54]=8705;
a[55]=9217;
a[56]=10241;
a[57]=11265;
a[58]=12289;
a[59]=13313;
a[60]=14337;
a[61]=15361;
a[62]=16385;
a[63]=17409;
a[64]=18433;
int n;
while(cin>>n)
{
cout<<a[n]<<endl;
}
return 0;
}

34
HDOJ/1208_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<stdio.h>
#include<string.h>
int main ()
{
__int64 dp[50][50];
int map[50][50];
int i,j,n;
char c;
while(~scanf("%d",&n)&&n!=-1)
{
getchar();
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%c",&c);
map[i][j]=c-48;
}
getchar();
}
memset(dp,0,sizeof(dp));
dp[1][1]=1;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
{
if(!map[i][j])
continue;
dp[i+map[i][j]][j]+=dp[i][j];
dp[i][j+map[i][j]]+=dp[i][j];
}
printf("%I64d\n",dp[n][n]);
}
return 0;
}

47
HDOJ/1209_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<set>
#include<map>
#include<string>
#include<cstdlib>
using namespace std;
struct node
{
int x,y;
double z;
};
node a[10];
bool cmp(node a,node b)
{
if(a.z==b.z)
{
if(a.x==b.x)return a.y<b.y;
return a.x<b.x;
}
return a.z<b.z;
}
int main()
{
int c,d,n,i,j,k,t,x,y;
double z;
while(~scanf("%d",&n))
{
while(n--)
{
for(i=0;i<5;i++)
{
scanf("%d:%d",&x,&y);
a[i].y=y;a[i].x=x;
if(x>=12)x%=12;
z=fabs(1.0*x*30+1.0*y/2-1.0*6*y);
a[i].z=min(360-z,z);
}
sort(a,a+5,cmp);
printf("%02d:%02d\n",a[2].x,a[2].y);
}
}
return 0;
}

20
HDOJ/1210_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include<stdio.h>
int main()
{
int m,x,ans;
while(~scanf("%d",&m))
{
x=1;
ans = 0;
do
{
if(x<=m)
x = 2*x;
else
x = 2*(x-m)-1;
ans++;
}
while(x!=1);
printf("%d\n",ans);
}
}

37
HDOJ/1211_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
__int64 n;
__int64 power(int a,int d)
{
int i;
__int64 res=1;
for(i=1;i<=d;i++)
res=(res*a)%n;
return res;
}
int main()
{
int d,i,j,p,q,e,l,a;
while(~scanf("%d%d%d%d",&p,&q,&e,&l))
{
n=(p-1)*(q-1);
for(i=1;;i++)
if((i*e)%n==1)
break;
d=i;
n=p*q;
for(i=0;i<l;i++)
{
scanf("%d",&a);
printf("%c",(power(a,d)));
}
putchar(10);
}
}

18
HDOJ/1212_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<stdio.h>
#include<string.h>
int main()
{
int n,i,t;
char a[1010];
while(scanf("%s%d",&a,&n)!=EOF)
{
t=0;
for(i=0;i<strlen(a);i++)
{
t=t*10+a[i]-'0';
t%=n;
}
printf("%d\n",t);
}
return 0;
}

37
HDOJ/1213_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<stdio.h>
#include<string.h>
int a[1003];
void ice(int x,int y)
{
a[x]=y;
}
int find(int x)
{
if(x!=a[x])
a[x]=find(a[x]);
return a[x];
}
int main()
{
int t,n,m,i,j,x,y;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
j=0;
for(i=1;i<=n;i++)
a[i]=i;
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
x=find(x);
y=find(y);
ice(x,y);
}
for(i=1;i<=n;i++)
if(a[i]==i)
j++;
printf("%d\n",j);
}
return 0;
}

12
HDOJ/1214_autoAC.cpp Normal file
View File

@ -0,0 +1,12 @@
#include<stdio.h>
int main()
{
int n,a;
while(~scanf("%d",&n))
{
a=n/2;
n-=a;
printf("%d\n",a*(a-1)/2+n*(n-1)/2);
}
return 0;
}

26
HDOJ/1215_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<stdio.h>
int main()
{
int n,i,a,j,sum,c;
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
{
sum=0;
scanf("%d",&a);
c=a;
for(j=2;j<c;j++)
if(a%j==0)
{
sum+=j;
if(j!=a/j)
{
sum+=a/j;
c=a/j;
}
}
sum++;
printf("%d\n",sum);
}
}
}

33
HDOJ/1216_autoAC.cpp Normal file
View File

@ -0,0 +1,33 @@
#include<stdio.h>
#include<string.h>
#define M 34000
int n,num[M],ss[3001],t=0;
int main()
{
int i,j,k;
memset(num,0,sizeof(num));
for(i=2;i<M;i++)
{
if(!num[i])
{
ss[t++]=i;k=0;
for(j=i+1;j<M;j++)
if(!num[j])
{
k++;
if(k==i)
{
k=0;
num[j]=1;
}
}
}
if(t==3000)
{break;}
}
while(scanf("%d",&n),n!=0)
{
printf("%d\n",ss[n-1]);
}
return 0;
}

77
HDOJ/1217_autoAC.cpp Normal file
View File

@ -0,0 +1,77 @@
#include<stdio.h>
#include<string.h>
#define max 100+10
double map[max][max];
int curr,t=1;
void floyd()
{
int k,i,j;
int exist=0;
for(k=1;k<=curr;k++)
{
for(i=1;i<=curr;i++)
{
for(j=1;j<=curr;j++)
{
if(map[i][j]<map[i][k]*map[k][j])
map[i][j]=map[i][k]*map[k][j];
}
}
}
for(i=1;i<=curr;i++)
{
if(map[i][i]>1)
{
exist=1;
break;
}
}
if(exist)
printf("Case %d: Yes\n",t++);
else
printf("Case %d: No\n",t++);
}
int main()
{
int m,i,j,x,y;
double rate;
char a[110],b[110],s[110][110];
while(scanf("%d",&curr)&&(curr!=0))
{
for(i=1;i<=curr;i++)
{
for(j=1;j<=curr;j++)
{
map[i][j]=0;
}
}
for(i=1;i<=curr;i++)
{
scanf("%s",s[i]);
}
scanf("%d",&m);
while(m--)
{
scanf("%s %lf %s",a,&rate,b);
for(i=1;i<=curr;i++)
{
if(strcmp(s[i],a)==0)
{
x=i;
break;
}
}
for(i=1;i<=curr;i++)
{
if(strcmp(s[i],b)==0)
{
y=i;
break;
}
}
map[x][y]=rate;
}
floyd();
}
return 0;
}

53
HDOJ/1218_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<stdio.h>
#include<string.h>
int main()
{
int i,j,R,C;
int a[10][10],b[10][10];
char c,str[10];
for(i=0;i<10;i++)
{
for(j=0;j<10;j++)
{
a[i][j]=0;
b[i][j]=0;
}
}
while(1)
{
scanf("%s",str);
if(strcmp(str,"ENDOFINPUT")==0)
break;
if(strcmp(str,"START")==0)
{
scanf("%d%d",&R,&C);
getchar();
for(i=0;i<R;i++)
{
for(j=0;j<C;j++)
{
scanf("%c",&c);
a[i][j]=c-'0';
}
getchar();
}
for(i=0;i<R-1;i++)
{
for(j=0;j<C-1;j++)
{
b[i][j]=(a[i][j]+a[i+1][j]+a[i][j+1]+a[i+1][j+1])/4;
}
}
for(i=0;i<R-1;i++)
{
for(j=0;j<C-1;j++)
{
printf("%d",b[i][j]);
}
printf("\n");
}
}
if(strcmp(str,"END")==0) continue;
}
return 0;
}

17
HDOJ/1219_autoAC.cpp Normal file
View File

@ -0,0 +1,17 @@
#include<stdio.h>
#include<memory.h>
int main ()
{
int s,i,b[26];
char a[100001];
while(gets(a)!=NULL)
{
memset(b,0,sizeof(b));
for(i=0;a[i]!='\0';i++)
b[a[i]-'a']++;
for(i=0;i<26;i++)
printf("%c:%d\n",'a'+i,b[i]);
printf("\n");
}
return 0;
}

8
HDOJ/1220_autoAC.cpp Normal file
View File

@ -0,0 +1,8 @@
#include <stdio.h>
int main(){
int n;
while(~scanf("%d",&n)){
printf("%d\n",n*n*(n-1)/2*(n*n*n-2)+n*n*n*(n*n-5)/2+2*n*n);
}
return 0;
}

28
HDOJ/1221_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <iostream>
#include <math.h>
using namespace std;
double a,b,r;
double xj(double xy,double x,double y,double aa,double bb)
{
double m,n=y+1;
m=r*r-(xy-aa)*(xy-aa);
if(m>0) n=sqrt(m)+bb;
if(m<0||n>y||n<x) return 0;
else return 1;
}
int main(){
int n;
scanf("%d",&n);
while(n--)
{
double x1,y1,x2,y2,a1,b1,c1,d1;
scanf("%lf%lf%lf%lf%lf%lf%lf",&a,&b,&r,&x1,&y1,&x2,&y2);
a1=x1>x2?x2:x1;b1=x1+x2-a1;
c1=y1>y2?y2:y1;d1=y1+y2-c1;
if(xj(a1,c1,d1,a,b)||xj(b1,c1,d1,a,b)||xj(c1,a1,b1,b,a)||xj(d1,a1,b1,b,a))
printf("YES\n");
else
printf("NO\n");
}
return 0;
}

20
HDOJ/1222_autoAC.cpp Normal file
View File

@ -0,0 +1,20 @@
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
__int64 gcd(__int64 a,__int64 b){
if(a<b)return gcd(b,a);
if(!b)return a;
return gcd(b,a%b);
}
int main(void){
__int64 n,T,m;
cin>>T;
while(T--){
cin>>m>>n;
if(gcd(m,n)==1)cout<<"NO\n";
else cout<<"YES\n";
}
return 0;
}

88
HDOJ/1223_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include <iostream>
#include <iomanip>
using namespace std;
struct LL
{
int num[50];
int len;
};
LL count[51][51];
LL total[51];
LL operator+(const LL& a, const LL& b)
{
LL c;
int Len = a.len>b.len?a.len:b.len;
int jin = 0;
for(int i=0;i<Len;i++)
{
c.num[i] = jin;
jin = 0;
if(i<a.len)
c.num[i] += a.num[i];
if(i<b.len)
c.num[i] += b.num[i];
jin = c.num[i]/1000000;
c.num[i]%=1000000;
}
c.len = Len;
if(jin)
{
c.num[Len] = jin;
c.len++;
}
return c;
}
LL operator*(const LL& a, int num)
{
LL c;
int Len = a.len;
int jin = 0;
for(int i=0;i<Len;i++)
{
c.num[i] = jin;
jin = 0;
c.num[i]+=a.num[i]*num;
jin = c.num[i]/1000000;
c.num[i]%=1000000;
}
c.len = Len;
if(jin)
{
c.num[Len] = jin;
c.len++;
}
return c;
}
ostream& operator<<(ostream& out, const LL& a)
{
out << a.num[a.len-1];
for(int i=a.len-2;i>=0;i--)
{
out << setfill('0') << setw(6) << a.num[i];
}
return out;
}
int main()
{
count[1][0].num[0] = 1;
count[1][0].len = 1;
total[1].num[0] = 1;
total[1].len = 1;
for(int i=2;i<51;i++)
{
total[i] = count[i][0] = count[i-1][0] * i;
for(int j=1;j<i;j++)
{
count[i][j] = (count[i-1][j-1]+count[i-1][j]) * (i-j);
total[i]=total[i]+count[i][j];
}
}
int P;
cin >> P;
while(P--)
{
int k;
cin >> k;
cout << total[k] << endl;
}
}

60
HDOJ/1224_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <cstdlib>
#include <iostream>
using namespace std;
const int maxn=102;
int d[maxn];
bool map[maxn][maxn];
int w[maxn];
int pre[maxn];
void print(int x)
{
if(pre[x]!=0)
print(pre[x]);
printf("%d->",x);
}
int main()
{
int t,n,m,x,y,l,i,j;
scanf("%d",&t);
for(l=1;l<=t;l++)
{
scanf("%d",&n);
for(i=1;i<=n;i++) scanf("%d",&d[i]);
d[n+1]=0;
for(i=1;i<=n+1;i++)
{
w[i]=0;
pre[i]=0;
for(j=i;j<=n+1;j++)
{
map[i][j]=false;
}
}
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
map[x][y]=true;
}
for(j=1;j<=n+1;j++)
{
int max=0;
int k=0;
for(i=1;i<j;i++)
{
if(map[i][j]&&max<=w[i])
{
max=w[i];
k=i;
}
}
pre[j]=k;
w[j]=max+d[j];
}
printf("CASE %d#\npoints : %d\ncircuit : ",l,w[n+1]);
print(pre[n+1]);
printf("1\n");
if(l<t) printf("\n");
}
return 0;
}

107
HDOJ/1225_autoAC.cpp Normal file
View File

@ -0,0 +1,107 @@
#include <stdio.h>
#include <string.h>
typedef struct
{
char name[30];
int score;
int kick;
int lost;
}TEAM;
int is_exist(int len,TEAM *p,const char *s)
{
int i;
for(i = 0 ; i < len ; ++i)
{
if(!strcmp(p[i].name,s))
return 1;
}
return 0;
}
int find(TEAM *p,int len,const char *s)
{
int i;
for(i = 0 ; i < len ; ++i)
{
if(!strcmp(p[i].name,s))
return i;
}
return -1;
}
void swap(TEAM *p1,TEAM *p2)
{
TEAM t = *p1;
*p1 = *p2;
*p2 = t;
}
void sort(TEAM *p,const int LEN)
{
int i,j;
for(i = 0 ; i < LEN ; ++i)
{
for(j = i + 1 ; j < LEN ; ++j)
{
if(p[i].score < p[j].score)
swap(&p[i],&p[j]);
else if(p[i].score == p[j].score)
{
if(p[i].kick - p[i].lost < p[j].kick - p[j].lost)
swap(&p[i],&p[j]);
else if(p[i].kick - p[i].lost == p[j].kick - p[j].lost)
{
if(p[i].kick < p[j].kick)
swap(&p[i],&p[j]);
else if(p[i].kick == p[j].kick)
{
if(strcmp(p[i].name,p[j].name) > 0)
swap(&p[i],&p[j]);
}
}
}
}
}
}
int main(int argc, char *argv[])
{
int T,a,b;
char t1[30],t2[30],vs[5],fen[10];
TEAM team[5010];
while(scanf("%d",&T) != EOF)
{
memset(team,0,sizeof(team));
int t = T * (T - 1);
int ix_team = 0;
while(t--)
{
scanf("%s%s%s%s",t1,vs,t2,fen);
sscanf(fen,"%d:%d",&a,&b);
if(ix_team < T)
{
if(0 == is_exist(ix_team,team,t1))
strcpy(team[ix_team++].name,t1);
if(0 == is_exist(ix_team,team,t2))
strcpy(team[ix_team++].name,t2);
}
int ix1 = find(team,ix_team,t1);
int ix2 = find(team,ix_team,t2);
int score;
if(a > b) score = 3;
else if(a == b) score = 1;
else score = 0;
team[ix1].kick += a;
team[ix1].lost += b;
team[ix1].score += score;
if(b > a) score = 3;
else if(b == a) score = 1;
else score = 0;
team[ix2].kick += b;
team[ix2].lost += a;
team[ix2].score += score;
}
sort(team,T);
int i;
for(i = 0 ; i < T ; ++i)
printf("%s %d\n",team[i].name,team[i].score);
printf("\n");
}
return 0;
}

73
HDOJ/1226_autoAC.cpp Normal file
View File

@ -0,0 +1,73 @@
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define MAXN 5000
#define MAXM 17
#define MAXC 17
using namespace std;
int num[MAXM];
bool status[MAXN];
int basis, sys, n;
struct Node{
int mod;
int dig;
int pre;
int step;
}Queue[MAXN], init = { 0, 0, -1 };
int cmp(const void *a, const void *b){
return *(int *)a - *(int *)b;
}
void echo(int x){
if (Queue[x].pre == -1) return;
else echo(Queue[x].pre);
if (Queue[x].dig <= 9)
printf("%d", Queue[x].dig);
else
putchar(Queue[x].dig + 'A' - 10);
}
bool bfs(){
Node curr, next;
int front = 0, rear = 1, res;
Queue[0] = init;
memset(status, 0, sizeof(status));
while (front != rear){
curr = Queue[front];
for (int i = 0; i < n; i++){
res = (curr.mod * sys + num[i]) % basis;
if (status[res] || (curr.pre == -1 && num[i] == 0) || curr.step >= 500) continue;
status[res] = 1;
Queue[rear].mod = res;
Queue[rear].dig = num[i];
Queue[rear].pre = front;
Queue[rear].step = curr.step + 1;
if (res == 0) { echo(rear); putchar('\n'); return true; }
rear++;
}
front++;
}
return false;
}
int main(){
int t;
char tmp[3];
scanf("%d", &t);
while (t--){
scanf("%d %d %d", &basis, &sys, &n);
for (int i = 0; i < n; i++){
scanf("%s", tmp);
if (tmp[0] <= '9' && tmp[0] >= '0') num[i] = tmp[0] - '0';
else if (tmp[0] <= 'F' && tmp[0] >= 'A') num[i] = tmp[0] - 'A' + 10;
}
qsort(num, n, sizeof(int), cmp);
if (basis == 0){
if (num[0] == 0)
printf("0\n");
else
printf("give me the bomb please\n");
continue;
}
if (!bfs())
printf("give me the bomb please\n");
}
return 0;
}

38
HDOJ/1227_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
#define inf 0x7fffffff
int abbs(int a) { return a<0 ? -a : a ; }
int food[205], dp[35][205], dis[205][205];
int main()
{
int n, m, i, j, k, mid, minn, p=1;
while(scanf("%d%d", &n, &m) != EOF )
{
if( n==0 && m==0 ) break;
for(i=1; i<=n; i++)
scanf("%d",&food[i]);
for(i=1; i<=n; i++)
{
for(j=i; j<=n; j++)
{
dis[i][j] = 0; mid = (i+j)/2;
for(k=i; k<=j; k++ )
dis[i][j] += abbs( food[k] - food[mid] ) ;
}
}
for( i=1; i<=n; i++ )
{
dp[1][i] = dis[1][i];
}
for(i=2; i<=m; i++ )
{
for(j=i; j<=n; j++ )
{
dp[i][j] = inf ;
for(k=i-1; k<j; k++ )
if( dp[i][j] > dp[i-1][k] + dis[k+1][j] )
dp[i][j] = dp[i-1][k] + dis[k+1][j] ;
}
}
printf("Chain %d\nTotal distance sum = %d\n\n" , p++, dp[m][n] );
}
}

61
HDOJ/1228_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int cmletter(char *s)
{
int op;
if(strcmp(s,"zero")==0) op=0;
if(strcmp(s,"one")==0)op=1;
if(strcmp(s,"two")==0) op=2;
if(strcmp(s,"three")==0) op=3;
if(strcmp(s,"four")==0) op=4;
if(strcmp(s,"five")==0) op=5;
if(strcmp(s,"six")==0) op=6;
if(strcmp(s,"seven")==0) op=7;
if(strcmp(s,"eight")==0) op=8;
if(strcmp(s,"nine")==0) op=9;
return op;
}
int dig(char *t,char *u)
{
int len1,s,r;
len1 = strlen(u);
if(len1>0)
r = 10*cmletter(t)+cmletter(u);
else
r = cmletter(t);
return r;
}
int main()
{
int i,j,k,tg,len,a,b;
char s[1000],t[16],u[16];
while(gets(s))
{
len = strlen(s);
tg = 0;
for(i=0,j=0,k=0;i<len;i++)
{
if(s[i]=='+')break;
if(islower(s[i])&&!tg)t[j++]=s[i];
if(islower(s[i])&&tg) u[k++]=s[i];
else if(isspace(s[i])) tg = 1;
}
t[j]='\0';
u[k]='\0';
a = dig(t,u);
tg = 0;
for(i=i+2,j=0,k=0;i<len;i++)
{
if(islower(s[i])&&!tg)t[j++]=s[i];
if(islower(s[i])&&tg) u[k++]=s[i];
else if(isspace(s[i])) tg = 1;
}
t[j]='\0';
u[k]='\0';
b = dig(t,u);
if(a+b)printf("%d\n",a+b);
else break;
}
return 0;
}

16
HDOJ/1229_autoAC.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <stdio.h>
int main()
{
int a,b,k;
while(scanf("%d%d%d",&a,&b,&k)!=EOF&&a&&b)
{
int m=1;
while(k--)
m*=10;
if(a%m==b%m)
printf("-1\n");
else
printf("%d\n",a+b);
}
return 0;
}

95
HDOJ/1230_autoAC.cpp Normal file
View File

@ -0,0 +1,95 @@
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<map>
using namespace std;
#define lson ((root<<1)+1)
#define rson ((root<<1)+2)
#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
const int maxn=150;
const int base=1000;
const int inf=999999;
int isp[maxn];
int p[maxn];
void mark()
{
int i,j;
int len=0;
isp[0]=isp[1]=1;
for(i=2;i<maxn;i++)
if(isp[i]==0)
{
p[len++]=i;
for(j=i+i;j<maxn;j+=i)
isp[j]=1;
}
}
int main()
{
int n,m,i,j,k,t;
mark();
int a[maxn],b[maxn];
int lena,lenb;
int x,y;
char op;
while(1)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
scanf("%d",&x);
i=0;
a[i++]=x;
scanf("%c",&op);
while(op==',')
{
scanf("%d",&x);
a[i++]=x;
scanf("%c",&op);
}
lena=i;
scanf("%d",&x);
i=0;
b[i++]=x;
scanf("%c",&op);
while(op==',')
{
scanf("%d",&x);
b[i++]=x;
scanf("%c",&op);
}
lenb=i;
reverse(a,a+lena);
reverse(b,b+lenb);
if(a[0]==0&&b[0]==0&&lena==1&&lenb==1)break;
int maxlen=max(lena,lenb);
int sum[maxn];
memset(sum,0,sizeof(sum));
for(i=0,k=0;i<maxlen;i++)
{
sum[k++]=a[i]+b[i];
}
int lensum=k;
for(i=0;i<lensum+10;i++)
{
int cur=sum[i]%p[i];
int mod=sum[i]/p[i];
sum[i]=cur;
sum[i+1]+=mod;
}
for(i=maxn-1;i>=0;i--)if(sum[i]!=0)break;
j=i;
printf("%d",sum[j]);
for(i=j-1;i>=0;i--)
printf(",%d",sum[i]);
printf("\n");
}
return 0;
}

36
HDOJ/1231_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <stdio.h>
int a[100000];
int main()
{
int n;
while(~scanf("%d",&n)&&n)
{
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
int thissum=0;
int maxsum=a[0];
int stop=a[0];
int temp=a[0];
int start=a[0];
for(int i=0;i<n;i++)
{
thissum+=a[i];
if(thissum>maxsum)
{
maxsum=thissum;
start=temp;
stop=a[i];
}
if(thissum<0)
{
thissum=0;
temp=a[i+1];
}
}
if(maxsum<0)
printf("0 %d %d\n",a[0],a[n-1]);
else
printf("%d %d %d\n",maxsum,start,stop);
}
return 0;
}

38
HDOJ/1232_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <stdio.h>
int p[1222];
int i;
int find(int x)
{
if(x!=p[x])
p[x]=find(p[x]);
return p[x];
}
int hebing(int x,int y)
{
return p[x]=y;
}
int main()
{
int n,m,x,y,x1,y1;
while(~scanf("%d%d",&n,&m)&&n)
{
for(i=1;i<=1222;i++)
p[i]=i;
for(i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
x1=find(x);
y1=find(y);
if(x1!=y1)
hebing(x1,y1);
}
int ans=0;
for(i=1;i<=n;i++)
{
if(p[i]==i)
ans++;
}
printf("%d\n",ans-1);
}
return 0;
}

63
HDOJ/1233_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<stdio.h>
#include<algorithm>
#define max 50*99+10
using namespace std;
int set[110];
struct line
{
int dis;
int start;
int end;
}num[max];
bool cmp(line a,line b)
{
return a.dis<b.dis;
}
int find(int p)
{
int child=p;
int t;
while(p!=set[p])
p=set[p];
while(child!=p)
{
t=set[child];
set[child]=p;
child=t;
}
return p;
}
void merge(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
int main()
{
int city,need;
int n,i,j;
while(scanf("%d",&city)&&(city!=0))
{
for(i=1;i<=city;i++)
set[i]=i;
n=city*(city-1)/2;
for(i=0;i<n;i++)
{
scanf("%d%d%d",&num[i].start,&num[i].end,&num[i].dis);
}
sort(num,num+n,cmp);
need=0;
for(i=0;i<n;i++)
{
if(find(num[i].start)!=find(num[i].end))
{
merge(num[i].start,num[i].end);
need+=num[i].dis;
}
}
printf("%d\n",need);
}
return 0;
}

50
HDOJ/1234_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include<stdio.h>
#include<string.h>
struct node
{
char id[100];
char qd[100];
char ql[100];
};
struct node s[1000];
int main()
{
int n,i,j,k,m,x1,x2;
char c[100];
while(scanf("%d",&n)!=EOF&&n)
{
while(n--)
{ x1=0;x2=0;
scanf("%d",&m);
getchar();
for(i=0;i<m;i++)
{
scanf("%s%s%s",s[i].id,s[i].qd,s[i].ql);
}
if(m==1)
{
printf("%s %s\n",s[0].id,s[0].id);
}
if(m>1)
{ strcpy(c,s[0].qd);
for(i=1;i<m;i++)
{
if(strcmp(c,s[i].qd)>0)
{
strcpy(c,s[i].qd);x1=i;
}
}
strcpy(c,s[0].ql);
for(i=1;i<m;i++)
{
if(strcmp(c,s[i].ql)<0)
{
strcpy(c,s[i].ql);x2=i;
}
}
printf("%s %s\n",s[x1].id,s[x2].id);
}
}
}
return 0;
}

24
HDOJ/1235_autoAC.cpp Normal file
View File

@ -0,0 +1,24 @@
#include<stdio.h>
int main ()
{
int N;
int i;
while(scanf("%d", &N)!=EOF)
{
if(N==0)
break;
int num[1200], T, p=0;
for(int i=0; i<N; i++)
{
scanf("%d", &num[i]);
}
scanf("%d", &T);
for(i=0; i<N; i++)
{
if(num[i]==T)
p++;
}
printf("%d\n", p);
}
return 0;
}

40
HDOJ/1236_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
struct stu{
string na;
double sco;
}s[1010];
bool cmp(stu a,stu b){
if(a.sco==b.sco)
return a.na<b.na;
return a.sco>b.sco;
}
int main(){
int n,i,j,m,x,ans;
double g,a[20];
while(scanf("%d",&n),n){
scanf("%d%lf",&m,&g);
for(i=1;i<=m;i++)
scanf("%lf",&a[i]);
ans=0;
for(i=0;i<n;i++){
cin>>s[i].na;
scanf("%d",&m);
s[i].sco=0;
for(j=0;j<m;j++){
scanf("%d",&x);
s[i].sco+=a[x];
}
if(s[i].sco>=g)
ans++;
}
printf("%d\n",ans);
sort(s,s+n,cmp);
for(i=0;i<ans;i++){
cout<<s[i].na;
printf(" %.lf\n",s[i].sco);
}
}
}

37
HDOJ/1237_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int main()
{
double num[205];
int i;
double s;
char f;
double sum;
while(cin>>s)
{
memset(num,'\0',sizeof(num));
num[0]=s;
i=0;
f=getchar();
if(f=='\n'&&s==0)break;
while(1)
{
cin>>f>>s;
if(f=='*')num[i]*=s;
else if(f=='/')num[i]/=s;
else if(f=='+')num[++i]=s;
else
{
num[++i]=-s;
}
if(getchar()=='\n')break;
}
sum=0;
for(i;i>=0;i--)
sum+=num[i];
printf("%.2lf\n",sum);
}
return 0;
}

48
HDOJ/1239_autoAC.cpp Normal file
View File

@ -0,0 +1,48 @@
#include<cstdio>
#include<cmath>
bool prime[10001];
void IsPrime(){
prime[0]=prime[1]=0;prime[2]=1;
for(int i=3;i<=10000;i++)
prime[i]=i%2==0?0:1;
int t=(int)sqrt(10000*1.0);
for(int i=3;i<=t;i++)
if(prime[i])
for(int j=i*i;j<10000;j+=2*i)
prime[j]=0;
}
int main()
{
IsPrime();
int pri[10010];
int num = 0;
for(int i = 0; i < 10000; i++)
{
if(prime[i])
pri[num++] = i;
}
int m, a, b;
while(scanf("%d%d%d", &m, &a, &b)!=EOF && m && a && b)
{
int maxx = 0, ii, jj;
for(int i = num-1; i >= 0; i--)
{
for(int j = i; j <= num-1; j++)
{
if(pri[j] > m || pri[i]*pri[j]>m || 1.0*pri[i]/pri[j]<1.0*a/b)
continue;
else
{
if(maxx < pri[i]*pri[j])
{
maxx = pri[i]*pri[j];
ii = pri[i];
jj = pri[j];
}
}
}
}
printf("%d %d\n", ii, jj);
}
return 0;
}

76
HDOJ/1240_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include<stdio.h>
#include<queue>
using namespace std;
char map[10][10][10];
int startx,starty,startz,endx,endy,endz;
int n;
struct node
{
int x,y,z,step;
friend bool operator <(node a,node b)
{
return a.step>b.step;
}
};
void bfs(int x1,int y1,int z1,int x2,int y2,int z2)
{
int k;
int move[6][3]={0,0,1,0,0,-1,0,1,0,0,-1,0,1,0,0,-1,0,0};
node now,next;
priority_queue<node>q;
now.x=x1;
now.y=y1;
now.z=z1;
now.step=0;
map[now.x][now.y][now.z]='X';
q.push(now);
while(!q.empty())
{
next=q.top();
q.pop();
if(next.x==x2&&next.y==y2&&next.z==z2)
{
printf("%d %d\n",n,next.step);
return ;
}
for(k=0;k<6;k++)
{
now.x=next.x+move[k][0];
now.y=next.y+move[k][1];
now.z=next.z+move[k][2];
if(now.x>=0&&now.x<n&&now.y>=0&&now.y<n&&now.z>=0&&now.z<n&&map[now.x][now.y][now.z]=='O')
{
now.step=next.step+1;
map[now.x][now.y][now.z]='X';
q.push(now);
}
}
}
printf("NO ROUTE\n");
}
int main()
{
int i,j,k;
char s[10];
char c;
while(scanf("%s %d",s,&n)!=EOF)
{
c=getchar();
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
for(k=0;k<n;k++)
{
scanf("%c",&map[i][j][k]);
}
c=getchar();
}
c=getchar();
}
scanf("%d%d%d%d%d%d",&startx,&starty,&startz,&endx,&endy,&endz);
scanf("%s",s);
bfs(startx,starty,startz,endx,endy,endz);
}
return 0;
}

45
HDOJ/1241_autoAC.cpp Normal file
View File

@ -0,0 +1,45 @@
#include<stdio.h>
#include<string.h>
char map[110][110];
int move[8][2]={1,0,-1,0,0,1,0,-1,1,1,-1,-1,1,-1,-1,1};
int h,w;
void dfs(int x,int y)
{
int next_x,next_y,i;
map[x][y]='*';
for(i=0;i<8;i++)
{
next_x=x+move[i][0];
next_y=y+move[i][1];
if(next_x>=0&&next_x<h&&next_y>=0&&next_y<w)
{
if(map[next_x][next_y]=='@')
{
dfs(next_x,next_y);
}
}
}
}
int main()
{
int i,j,sum;
while(scanf("%d%d",&h,&w)&&(h!=0||w!=0))
{
for(i=0;i<h;i++)
scanf("%s",map[i]);
sum=0;
for(i=0;i<h;i++)
{
for(j=0;j<w;j++)
{
if(map[i][j]=='@')
{
dfs(i,j);
sum++;
}
}
}
printf("%d\n",sum);
}
return 0;
}

103
HDOJ/1242_autoAC.cpp Normal file
View File

@ -0,0 +1,103 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<queue>
#include<iostream>
#define N 300
using namespace std;
typedef struct node
{
int x,y;
int time;
friend bool operator<(node n1,node n2)
{
return n2.time<n1.time;
}
}node;
int map[N][N];
int dir[4][2]={{1,0},{-1,0},{0,1},{0,-1}};
int n,m;
int x1,x2,y1,y2;
int judge(int x,int y)
{
if(x<0||x>=m||y<0||y>=n||map[y][x]==-1)
return 1;
return 0;
}
int bfs()
{
priority_queue<node>Q;
while(!Q.empty())
Q.pop();
node fir,nex;
fir.x=x1;
fir.y=y1;
fir.time=0;
map[y1][x1]=-1;
Q.push(fir);
while(!Q.empty())
{
fir=Q.top();
Q.pop();
if(fir.x==x2&&fir.y==y2)
{
return fir.time;
}
for(int i=0;i<4;i++)
{
nex.x=fir.x+dir[i][0];
nex.y=fir.y+dir[i][1];
if(judge(nex.x,nex.y))
continue;
nex.time=fir.time+map[nex.y][nex.x]+1;
map[nex.y][nex.x]=-1;
Q.push(nex);
}
}
return -1;
}
int main()
{
char c[N][N];
while(~scanf("%d%d",&n,&m))
{
for(int i=0;i<n;i++)
scanf("%s",c[i]);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(c[i][j]=='.')
{
map[i][j]=0;
continue;
}
if(c[i][j]=='#')
{
map[i][j]=-1;
continue;
}
if(c[i][j]=='x')
{
map[i][j]=1;
continue;
}
if(c[i][j]=='r')
{
map[i][j]=0;
x1=j,y1=i;
continue;
}
if(c[i][j]=='a')
{
map[i][j]=0;
x2=j,y2=i;
}
}
int step=bfs();
if(step!=-1)
printf("%d\n",step);
else
printf("Poor ANGEL has to stay in the prison all his life.\n");
}
return 0;
}

40
HDOJ/1243_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
int ha[30],dp[2100][2100];
int main()
{
int n,i,j,carry,len1,len2,k,sji;
char s1[50],s2[2010],s3[2010];
while(~scanf("%d",&n))
{
scanf("%s",s1);
for(i=0;i<n;i++)
{
scanf("%d",&carry);
ha[s1[i]-'a']=carry;
}
scanf("%s %s",s2,s3);
len1=strlen(s2);
len2=strlen(s3);
memset(dp,0,sizeof(dp));
for(i=1;i<=len2;i++)
{
for(j=1;j<=len1;j++)
{
if(s3[i-1] == s2[j-1])
{
dp[i][j]=max(dp[i][j-1],dp[i-1][j-1]+ha[s3[i-1]-'a']);
}
else
{
dp[i][j]=max(dp[i][j-1],dp[i-1][j]);
}
}
}
printf("%d\n",dp[len2][len1]);
}
return 0;
}

53
HDOJ/1244_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<stdio.h>
#include<string.h>
int dp[1005][1005];
int main ()
{
int fw[1005][2],l[1005],num[1005];
int i,j,k,s,n,m;
while(~scanf("%d",&n)&&n)
{
scanf("%d",&m);
for(s=0,i=1;i<=m;i++)
{
scanf("%d",&l[i]);
fw[i][0]=s+1;
s+=l[i];
}
for(s=0,i=m;i>=1;i--)
{
fw[i][1]=n-s;
s+=l[i];
}
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
memset(dp,0,sizeof(dp));
for(i=1;i<=m;i++)
{
s=0;
for(j=fw[i][0];j<=fw[i][1];j++)
{
if(j-fw[i][0]+1<=l[i])
{
s+=num[j];
if(j-fw[i][0]+1==l[i])
{
if(dp[i][j]<s+dp[i-1][j-l[i]])
for(k=j;k<=n;k++)
dp[i][k]=dp[i-1][j-l[i]]+s;
}
}
else
{
s+=num[j];
s-=num[j-l[i]];
if(dp[i][j]<s+dp[i-1][j-l[i]])
for(k=j;k<=n;k++)
dp[i][k]=dp[i-1][j-l[i]]+s;
}
}
}
printf("%d\n",dp[m][n]);
}
return 0;
}

94
HDOJ/1245_autoAC.cpp Normal file
View File

@ -0,0 +1,94 @@
#include<stdio.h>
#include<math.h>
#include<queue>
using namespace std;
const int MAXN=110;
const double MAXNUM=1e8;
int n,step[MAXN];
double x[MAXN],y[MAXN];
double d,dis[MAXN],map[MAXN][MAXN];
bool mark[MAXN];
double dist(int i,int j)
{
double X=(x[i]-x[j])*(x[i]-x[j]);
double Y=(y[i]-y[j])*(y[i]-y[j]);
return sqrt(X+Y);
}
void SPFA()
{
int i;
queue<int> q;
for(i=1;i<=n+1;i++)
{
dis[i]=MAXNUM;
mark[i]=0;
}
step[0]=0;
dis[0]=0.0;
q.push(0);
mark[0]=1;
while(!q.empty())
{
int s=q.front();
q.pop();
mark[s]=0;
for(i=1;i<=n+1;i++)
{
if(map[s][i]<=d&&map[s][i]+dis[s]<dis[i])
{
dis[i]=map[s][i]+dis[s];
step[i]=step[s]+1;
if(!mark[i])
q.push(i);
}
else if(map[s][i]<=d&&dis[i]==map[s][i]+dis[s])
{
if(step[i]>step[s]+1)
{
step[i]=step[s]+1;
if(!mark[i])
q.push(i);
}
}
}
}
}
int main()
{
while(scanf("%d%lf",&n,&d)==2)
{
int i,j;
x[0]=y[0]=0;
for(i=1;i<=n;i++)
scanf("%lf%lf",x+i,y+i);
if(d>=42.50)
{
puts("42.50 1");
continue;
}
for(i=1;i<=n;i++)
{
map[i][i]=MAXNUM;
for(j=i+1;j<=n;j++)
map[i][j]=map[j][i]=dist(i,j);
}
for(i=1;i<=n;i++)
{
map[i][0]=0.0;
map[0][i]=sqrt(x[i]*x[i]+y[i]*y[i])-7.5;
}
for(i=1;i<=n;i++)
{
map[n+1][i]=0.0;
if(50.00-fabs(x[i])>50.00-fabs(y[i]))
map[i][n+1]=50.00-fabs(y[i]);
else map[i][n+1]=50.00-fabs(x[i]);
}
map[0][n+1]=MAXNUM;
SPFA();
if(dis[n+1]==MAXNUM)
puts("can't be saved");
else printf("%.2lf %d\n",dis[n+1],step[n+1]);
}
return 0;
}

21
HDOJ/1246_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
int i,a[310]={1,1},n,j;
for(i=3;i<310;i+=2)
{
for(j=310;j>=0;j--)
{
if(i+j<=310)
a[i+j]+=a[j];
}
}
while(cin>>n)
{
cout<<a[n]<<endl;
}
return 0;
}

60
HDOJ/1247_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include<iostream>
#include<cstring>
using namespace std;
char a[50010][123];
struct node{
bool p;
node *next[26];
node(){
memset(next,NULL,sizeof(next));
p=0;
}
}*head;
void insert(char *c){
int i=0;
node *p=head;
while(c[i]){
if(p->next[c[i]-'a']==NULL)
p->next[c[i]-'a']=new node;
p=p->next[c[i]-'a'];
i++;
}
p->p=true;
}
bool search(char *c){
node *p=head,*s;
int i,j,l=strlen(c),o;
char k;
for(i=0;i<l;i++){
k=c[i]-'a';
if(p->next[k]){
p=p->next[k];
if(p->p==true){
s=head;o=1;
for(j=i+1;j<l;j++){
k=c[j]-'a';
if(s->next[k])
s=s->next[k];
else{
o=0;
break;
}
}
if(o){
if(s->p)
return 1;
}
}
}
}
return 0;
}
int main(){
int i,j,n=0;
head=new node;
while(~scanf("%s",a[n++]))
insert(a[n-1]);
for(i=0;i<n;i++)
if(search(a[i]))
printf("%s\n",a[i]);
}

26
HDOJ/1248_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int temp;
cin>>temp;
int m1=temp/150;
int m2=temp/200;
int m3=temp/350;
int sum=temp;
for(int i=0;i<=m1;++i)
for(int j=0;j<=m2;++j)
for(int k=0;k<=m3;++k)
{
int sum1=temp-150*i-200*j-350*k;
if(sum1<sum&&sum1>=0)
sum=sum1;
}
cout<<sum<<endl;
}
return 0;
}

13
HDOJ/1249_autoAC.cpp Normal file
View File

@ -0,0 +1,13 @@
#include<stdio.h>
int main()
{
int n,s=2,i,m;
scanf("%d",&n);
while(n--)
{
scanf("%d",&m);
s+=6*(m-1);
printf("%d\n",s);
}
return 0;
}

44
HDOJ/1250_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<iostream>
#include<cstring>
using namespace std;
short x[5][2008];
int main()
{
int n,i,j;
while(cin>>n)
{
if(n<=4)
{
cout<<1<<endl;
continue;
}
memset(x,0,sizeof(x));
x[1][0]=1;
x[2][0]=1;
x[3][0]=1;
x[4][0]=1;
for(i=5;i<=n;++i)
{
memset(x[i%5],0,sizeof(x[i%5]));
for(j=0;j<=2005;++j)
{
x[i%5][j]+=x[(i-1)%5][j]+x[(i-2)%5][j]+x[(i-3)%5][j]+x[(i-4)%5][j];
if(x[i%5][j]>=10)
{
x[i%5][j+1]+=x[i%5][j]/10;
x[i%5][j]%=10;
}
}
}
i=2005;
while(x[n%5][i]==0)
--i;
while(i>=0)
{
cout<<x[n%5][i];
--i;
}
cout<<endl;
}
return 0;
}

27
HDOJ/1251_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<stdio.h>
#include<string.h>
char str[28][11111][11];
int main()
{
char s[11],tem[11];
int i=0,j,num[28];
int count;
int len;
memset(num,0,sizeof(num));
while(gets(tem)&&strcmp(tem,"")!=0)
{
int r=tem[0]-96; strcpy(str[r][num[r]++] , tem);
}
while(scanf("%s",s)!=EOF)
{
count=0;int r=s[0]-96;
len=strlen(s);
for(j=0;j<num[r];j++)
{
if(memcmp(str[r][j],s,len)==0)
count++;
}
printf("%d\n",count);
}
return 0;
}

90
HDOJ/1252_autoAC.cpp Normal file
View File

@ -0,0 +1,90 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,ok,p1,p2,p3;
int ans[51][51][51];
char map[51][51];
struct Hike{
char a,b,c;
};
void bfs()
{
int i,a,b,c;
queue<Hike>Q;
Hike tmp;
ans[p1][p2][p3] = 0;
tmp.a = p1;
tmp.b = p2;
tmp.c = p3;
Q.push(tmp);
while(!Q.empty())
{
tmp = Q.front(); Q.pop();
a = tmp.a; b = tmp.b; c = tmp.c;
if((a == b) && (a == c))
{
ok = a; break;
}
int current = ans[a][b][c];
current ++;
char *color ;
char bcColor = map[b][c];
color = map[a]+1;
for(i=1;i<=n;i++,color++)
{
if(i != a && *color == bcColor && ans[i][b][c] > current)
{
ans[i][b][c] = current;
tmp.a = i; tmp.b = b; tmp.c = c;
Q.push(tmp);
}
}
char acColor = map[a][c];
color = map[b] + 1;
for(i=1;i<=n;i++,color++)
{
if(i != b && *color == acColor && ans[a][i][c] > current)
{
ans[a][i][c] = current;
tmp.a = a; tmp.b = i; tmp.c = c;
Q.push(tmp);
}
}
char abColor = map[a][b];
color = map[c] + 1;
for(i=1;i<=n;i++,color++)
{
if(i != c && *color == abColor && ans[a][b][i] > current)
{
ans[a][b][i] = current;
tmp.a = a; tmp.b = b; tmp.c = i;
Q.push(tmp);
}
}
}
}
int main()
{
int i,j;
char str[110];
while(scanf("%d",&n)!=EOF && n)
{
scanf("%d%d%d\n",&p1,&p2,&p3);
for(i=1;i<=n;i++)
{
gets(str);
for(j=0;j<n;j++)
sscanf(str+2*j,"%c",&map[i][j+1]);
map[i][n+1] = '\0';
}
ok = 0;
fill(&ans[0][0][0],&ans[0][0][0]+51*51*51,255);
bfs();
if(ok) printf("%d\n",ans[ok][ok][ok]);
else printf("impossible\n");
}
return 0;
}

65
HDOJ/1253_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <sstream>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <stack>
#include <set>
#include <numeric>
#include <utility>
#include <cstring>
using namespace std;
int A, B, C, T;
struct point {
int x, y, z;
int t;
point(int x=0, int y=0, int z=0, int t=0): x(x), y(y), z(z), t(t) {}
};
int direct[][3] = {{1,0,0}, {-1,0,0}, {0,0,1}, {0,0,-1}, {0,1,0}, {0,-1,0}};
int Map[55][55][55];
int BFS() {
queue<point> que;
que.push(point(0,0,0,0));
while(que.size()) {
point p = que.front();
que.pop();
if(p.t > T)
continue;
if(p.x == A-1 && p.y == B-1 && p.z == C-1)
return p.t;
for(int k=0; k<6; ++k) {
int nx = p.x + direct[k][0];
int ny = p.y + direct[k][1];
int nz = p.z + direct[k][2];
if(nx >=0 && nx<A && ny>=0 && ny<B && nz>=0 && nz<C && Map[nx][ny][nz] == 0) {
que.push(point(nx, ny, nz, p.t+1));
Map[nx][ny][nz] = 1;
}
}
}
return - 1;
}
int main()
{
int K;
scanf("%d", &K);
while(K--) {
scanf("%d%d%d%d", &A, &B, &C, &T);
for(int a=0; a<A; ++a)
for(int b=0; b<B; ++b)
for(int c=0; c<C; ++c)
scanf("%d", &Map[a][b][c]);
printf("%d\n", BFS());
}
return 0;
}

106
HDOJ/1254_autoAC.cpp Normal file
View File

@ -0,0 +1,106 @@
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<queue>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-6
using namespace std;
int g[8][8];
int xx[]={1,-1,0,0};
int yy[]={0,0,-1,1};
int n,m;
struct node
{
int x,y,lv;
};
node box,man,over;
bool vis[8][8];
bool ok;
void dfs(int x1,int y1,int x2,int y2)
{
if(ok)return;
if(x1==x2&&y1==y2)
{
ok=1;
return;
}
int x,y;
for(int k=0;k<4;k++)
{
x=x1+xx[k];
y=y1+yy[k];
if(x>=n||x<0||y>=m||y<0||g[x][y]==1||vis[x][y])
continue;
vis[x][y]=1;
dfs(x,y,x2,y2);
}
}
int bfs()
{
bool way[8][8][8][8];
queue<node>q;
queue<node>human;
memset(way,0,sizeof(way));
q.push(box);
human.push(man);
way[box.x][box.y][man.x][man.y]=1;
node tmp,now,ma;
while(!q.empty())
{
tmp=q.front();q.pop();
ma=human.front();human.pop();
if(tmp.x==over.x&&tmp.y==over.y)return tmp.lv;
for(int k=0;k<4;k++)
{
int x=tmp.x+xx[k];
int y=tmp.y+yy[k];
if(x>=n||x<0||y>=m||y<0||g[x][y]==1||way[x][y][tmp.x][tmp.y])
continue;
int rex=tmp.x-xx[k];
int rey=tmp.y-yy[k];
if(rex>=n||rex<0||rey>=m||rey<0||g[rex][rey]==1)
continue;
memset(vis,0,sizeof(vis));
ok=0;
vis[tmp.x][tmp.y]=1;
dfs(ma.x,ma.y,rex,rey);
if(!ok)
continue;
now.x=x,now.y=y,now.lv=tmp.lv+1;
way[x][y][tmp.x][tmp.y]=1;
q.push(now);
human.push(tmp);
}
}
return -1;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
scanf("%d",&g[i][j]);
if(g[i][j]==2)
box.x=i,box.y=j,box.lv=0;
else if(g[i][j]==3)
over.x=i,over.y=j;
else if(g[i][j]==4)
man.x=i,man.y=j;
}
int tmp=bfs();
printf("%d\n",tmp);
}
}

91
HDOJ/1255_autoAC.cpp Normal file
View File

@ -0,0 +1,91 @@
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int MAXN=1003;
double y[2*MAXN];
struct Line
{
double x;
double up,down;
int mark;
bool operator <(const Line a)const
{
return x<a.x;
}
}L[2*MAXN];
struct node
{
double x;
double up,down;
int cover;
bool isLeaf;
}T[20*MAXN];
void build(int k,int left,int right)
{
T[k].x=-1;
T[k].down=y[left];
T[k].up=y[right];
T[k].cover=0;
if(left+1==right)
{
T[k].isLeaf=true;
return;
}
T[k].isLeaf=false;
int mid=(left+right)/2;
build(2*k+1,left,mid);
build(2*k+2,mid,right);
}
double insert(int k,double x,double down,double up,int mark)
{
if(T[k].down>=up||T[k].up<=down)
return 0;
if(T[k].isLeaf)
{
double s=0;
if(T[k].cover>1)
{
double xx=T[k].x;
s=(x-xx)*(T[k].up-T[k].down);
}
T[k].x=x;
T[k].cover+=mark;
return s;
}
double s1=insert(2*k+1,x,down,up,mark);
double s2=insert(2*k+2,x,down,up,mark);
return s1+s2;
}
int main()
{
int T,N;
double area,x1,x2,y1,y2;
scanf("%d",&T);
while(T--)
{
area=0;
scanf("%d",&N);
for(int i=0,num=-1;i<N;++i)
{
scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
L[++num].x=x1;
L[num].down=y1;
L[num].up=y2;
L[num].mark=1;
y[num]=y1;
L[++num].x=x2;
L[num].down=y1;
L[num].up=y2;
L[num].mark=-1;
y[num]=y2;
}
sort(L,L+2*N);
sort(y,y+2*N);
build(0,0,2*N-1);
for(int i=0;i<2*N;++i)
area+=insert(0,L[i].x,L[i].down,L[i].up,L[i].mark);
printf("%.2f\n",area);
}
return 0;
}

41
HDOJ/1256_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include<stdio.h>
int main()
{
int T,n,a,b,i,j,g=0;
char _char;
scanf("%d",&T);
while(T--)
{
if(g!=0)printf("\n");
g=1;
getchar();
_char=getchar();
scanf("%d",&n);
a=(n/6)+1 ;
b=(n-3)/2 ;
for(i=1;i<=n;i++)
{
for(j=1 ; j<=2*a+n-3-b ; j++)
{
if(i==1||i==(2+b)||i==n)
{
if(j==a-b+n-2)
{
break;
}
if(j<a+1) putchar(' ');
else
putchar(_char);
}
else
{
if(j>a&&j<a-b+n-2)
putchar(' ');
else putchar(_char);
}
}
printf("\n");
}
}
return 0;
}

29
HDOJ/1257_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include<stdio.h>
#include<string.h>
int a[1000008],b[1000008];
int main()
{
int n,i,k,j;
while(scanf("%d",&n)==1)
{
k=0;
scanf("%d",&a[0]);
b[k]=a[0];
for(i=1;i<n;i++)
{
scanf("%d",&a[i]);
for(j=0;j<=k;j++)
if(b[j]>a[i])
{
b[j]=a[i];
break;
}
if(j>k)
{
b[++k]=a[i];
}
}
printf("%d\n",k+1);
}
return 0;
}

68
HDOJ/1258_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int g[14][14];
int visited[14];
int vec[14];
int t,n;
int path[14];
int rear;
int flag;
void DFS(int start,int sum)
{
int j;
visited[start]=1;
path[rear]=vec[start];
if(sum>t)return ;
if(sum==t)
{
flag=1;
for(j=0;j<rear;j++)
cout<<path[j]<<"+";
cout<<path[j]<<endl;
return ;
}
for(j=start+1;j<=n;j++)
{
if(j!=start&&0==visited[j])
{
sum+=vec[j];
rear++;
DFS(j,sum);
sum-=vec[j];
rear--;
visited[j]=0;
}
while(j+1<=n&&vec[j]==vec[j+1])
j++;
}
}
int main()
{
int i,j,w;
while(cin>>t>>n&&!(0==t&&0==n))
{
for(i=1;i<=n;i++)
{
cin>>w;
vec[i]=w;
for(j=1;j<=n;j++)
g[i][j]=w;
}
i=1;
cout<<"Sums of "<<t<<":"<<endl;
flag=0;
for(i=1;i<=n;i++)
{
rear=0;
memset(visited,0,sizeof(visited));
DFS(i,vec[i]);
while(i+1<=n&&vec[i]==vec[i+1])
i++;
}
if(flag==0)
cout<<"NONE"<<endl;
}
return 0;
}

21
HDOJ/1259_autoAC.cpp Normal file
View File

@ -0,0 +1,21 @@
#include <stdio.h>
int main()
{
int n,i,t1,t2;
scanf("%d",&n);
while(n--)
{
int sum=2;
int m;
scanf("%d",&m);
for(i=1;i<=m;i++)
{
scanf("%d%d",&t1,&t2);
if(t2==sum)
sum=t1;
else if(t1==sum)
sum=t2;
}
printf("%d\n",sum);
}
}

46
HDOJ/1260_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<stdio.h>
#include<string.h>
int minn(int x,int y)
{
return x<y?x:y;
}
int main ()
{
int dp[2005];
int a1[2005];
int a2[2005];
int i,n,t,j;
int a,b,c;
char c1,c2;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
scanf("%d",&a1[i]);
for(i=1;i<n;i++)
scanf("%d",&a2[i]);
memset(dp,0,sizeof(dp));
dp[1]=a1[1];
for(i=2;i<=n;i++)
dp[i]=minn(dp[i-1]+a1[i],dp[i-2]+a2[i-1]);
a=8,b=c=0;
c=dp[n]%60;
b=dp[n]/60%60;
a+=(dp[n]/3600);
if(a<12||a==12&&b==c&&c==0)
{
c1='a';
c2='m';
}
else
{
if(a!=12)
a%=12;
c1='p';
c2='m';
}
printf("%02d:%02d:%02d %c%c\n",a,b,c,c1,c2);
}
return 0;
}

46
HDOJ/1261_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include<iostream>
#include<cstring>
using namespace std;
int a[500];
void change(int n,int m)
{
int c;
c=0;
for(int j=0;j<500;j++)
{
c=a[j]*n+c;
a[j]=c%10;
c=c/10;
}
c=0;
for(int j=499;j>=0;j--)
{
c=a[j]+c*10;
a[j]=c/m;
c=c%m;
}
return;
}
int main()
{
int n,s,k;
int b[26];
while(cin>>n&&n)
{
s=0;
memset(a,0,sizeof(a));
a[0]=1;
for(int i=0;i<n;i++){
cin>>b[i];
for(int j=1;j<=b[i];j++)
change(s+j,j);
s+=b[i];
}
for(k=499;k>=0;k--)if(a[k]!=0)
break;
for(int i=k;i>=0;i--)
cout<<a[i];
cout<<endl;
}
return 0;
}

30
HDOJ/1262_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<iostream>
#include<string.h>
using namespace std;
#define M 10005
char a[M]={1,1,0};
void prime()
{
int i,j;
for(i=2;i<M;i++)
{
if(!a[i])
for(j=i+i;j<M;j+=i)
a[j]=1;
}
}
int main()
{
prime();
int n;
while(cin>>n)
{
int i=n/2;
while(a[i]+a[n-i])i--;
cout<<i<<' '<<n-i<<endl;
}
return 0;
}

52
HDOJ/1263_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<string.h>
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
struct node
{
string friut,place;
int num;
}f[109];
bool cmp(node p,node q)
{
if(p.place!=q.place)
return p.place<q.place;
return p.friut<q.friut;
}
int main()
{
int T,n,j,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=1;i<=n;i++)
cin>>f[i].friut>>f[i].place>>f[i].num;
f[n+1].place="zzzzzzzzzzzz";
sort(f+1,f+1+n,cmp);
int last=1;
for(i=1;i<=n;i++)
{
if(f[i].place!=f[i+1].place)
{
cout<<f[i].place<<endl;;
int sum=f[last].num;
for(j=last+1;j<=i;j++)
{
if(f[j].friut!=f[j-1].friut)
{
cout<<" |----"<<f[j-1].friut<<"("<<sum<<")"<<endl;
sum=f[j].num;
}
else
sum+=f[j].num;
}
cout<<" |----"<<f[i].friut<<"("<<sum<<")"<<endl;
last=i+1;
}
}
if(T)puts("");
}
return 0;
}

49
HDOJ/1264_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
#define SWAP(a,b){int t=a;a=b;b=t;}
int hash[102][102];
void HashTable(int x1,int y1,int x2,int y2)
{
for(int i = x1;i < x2;i++)
for(int j = y1;j < y2;j++)
hash[i][j] = 1;
}
int CountHash(int minx,int miny,int maxx,int maxy)
{
int cnt = 0;
for(int i = minx;i < maxx;i++)
for(int j = miny;j < maxy;j++)if(hash[i][j])
{
cnt++;
}
return cnt;
}
int main()
{
int x1,x2,y1,y2,minx,miny,maxx,maxy;
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2))
{
memset(hash,0,sizeof(hash));
if(x1+x2+y1+y2==-8)break;
if(x1+x2+y1+y2==-4){puts("0");continue;}
if(x1>x2)SWAP(x1,x2);
if(y1>y2)SWAP(y1,y2);
minx = x1; miny = y1;
maxx = x2; maxy = y2;
HashTable(x1,y1,x2,y2);
while(scanf("%d%d%d%d",&x1,&y1,&x2,&y2),(x1+x2+y1+y2!=-4&&x1+x2+y1+y2!=-8))
{
if(x1>x2)SWAP(x1,x2);
if(y1>y2)SWAP(y1,y2);
if(minx>x1)minx = x1;
if(miny>y1)miny = y1;
if(maxx<x2)maxx = x2;
if(maxy<y2)maxy = y2;
HashTable(x1,y1,x2,y2);
}
int cnt=CountHash(minx,miny,maxx,maxy);
printf("%d\n",cnt);
if(x1+x2+y1+y2==-8)break;
}
return 0;
}

18
HDOJ/1265_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
char s[20];
float d;
long x,n;
scanf("%d",&n);
while(n--){
scanf("%f",&d);
memcpy(&x,&d,4);
printf("%X\n",x);
}
return 0;
}

36
HDOJ/1266_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<iostream>
#include<string>
using namespace std;
int main()
{
int n;
while(cin>>n)
{
string temp;
while(n--)
{
cin>>temp;
auto b=temp.begin();
if(*b=='-')
{
cout<<'-';
temp.erase(b);
}
int c=0;
auto d=temp.crbegin();
for(;d!=temp.crend();++d)
{
if(*d=='0')
c++;
else
break;
}
for(d;d!=temp.crend();++d)
cout<<*d;
for(int i=0;i!=c;++i)
cout<<'0';
cout<<endl;
}
}
return 0;
}

30
HDOJ/1267_autoAC.cpp Normal file
View File

@ -0,0 +1,30 @@
#include<cstdio>
#include<cstring>
__int64 d[1010][1010];
int fun()
{
int i,j;
memset(d,0,sizeof(d));
for(i=1; i<=20; i++)
{
d[i][1]=i;
}
for(i=2; i<=20; i++)
{
for(j=2; j<=20; j++)
{
if(i>=j)
d[i][j] = d[i][j-1]+d[i-1][j];
}
}
}
int main()
{
int m,n;
fun();
while(scanf("%d %d",&m,&n)!=EOF)
{
printf("%I64d\n",d[m][n]);
}
return 0;
}

52
HDOJ/1268_autoAC.cpp Normal file
View File

@ -0,0 +1,52 @@
#include<stdio.h>
#include<string.h>
#define i64 __int64
int Max(int a,int b){return a>b?a:b;}
int Min(int a,int b){return a<b?a:b;}
int n,m;
int main()
{
while(scanf("%d %d",&n,&m)!=EOF)
{
int hash[5555][2];
int i,maxw,maxl,j;
i64 a_min,a_max,temp;
a_min=a_max=0;
maxw=maxl=0;
for(i=0;i<5555;i++)hash[i][0]=hash[i][1]=0;
for(i=0;i<n;i++)
{
scanf("%d",&temp);
maxw=Max(temp,maxw);
hash[temp][0]++;
}
for(i=0;i<m;i++)
{
scanf("%d",&temp);
maxl=Max(temp,maxl);
hash[temp][1]++;
}
if(maxw!=maxl)
{
printf("No solution.\n");
continue;
}
for(i=1;i<=maxw;i++)
{
if(hash[i][0]||hash[i][1])
{
a_min+=Max(hash[i][0],hash[i][1])*i;
}
temp=0;
if(hash[i][0])
{
for(j=1;j<=maxl;j++)
{
if(hash[j][1])temp+=Min(i,j)*hash[j][1];
}
}
a_max+=temp*hash[i][0];
}
printf("%I64d %I64d\n",a_min,a_max);
}
}

57
HDOJ/1269_autoAC.cpp Normal file
View File

@ -0,0 +1,57 @@
#include<stdio.h>
#include<string.h>
int pre[2][100010];
int n,m;
int find(int a,int i)
{
int r=a;
while(r!=pre[i][r])
{
r=pre[i][r];
}
return r;
}
void fun(int a,int b)
{
if(a!=n)
{
int fa=find(a,0),fb=find(b,0);
if(fa!=fb)
pre[0][a]=b;
}
if(b!=n)
{
int fa=find(a,1),fb=find(b,1);
if(fa!=fb)
pre[1][b]=a;
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF,n||m)
{
int i,w=1;
for(i=0;i<=n;i++)
{
pre[0][i]=pre[1][i]=i;
}
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
fun(a,b);
}
for(i=1;i<=n;i++)
{
if(find(i,0)!=n||find(i,1)!=n)
{
w=0;
break;
}
}
if(w)
printf("Yes\n");
else
printf("No\n");
}
}

58
HDOJ/1270_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define MAXN 11000
int n, sum[MAXN], num[MAXN],tn[MAXN];
void init()
{
int i;
for (i = 1; i * 2 <= n * (n - 1); ++i)
scanf("%d", &sum[i]);
}
void work()
{
int i,j,k;
for (num[1] = 1; num[1] * 2 <= sum[1]; ++num[1])
{
j=2;
memset(tn, 0, sizeof(tn));
for(i=1;i*2<=n*(n-1);i++)
{
tn[sum[i]]++;
}
for(i=1;i*2<=n*(n-1);i++)
{
if(tn[sum[i]]>0)
{
num[j]=sum[i]-num[1];
for(k=1;k<j;k++)
{
if(tn[num[k]+num[j]]==0)
break;
else
tn[num[k]+num[j]]--;
}
if(k==j)
j++;
else
break;
}
}
if(j>n)
break;
}
if(j<=n)
return ;
for(i=1;i<n;i++)
printf("%d ",num[i]);
printf("%d\n",num[n]);
}
int main()
{
while (scanf("%d", &n) != EOF && n)
{
init();
work();
}
return 0;
}

43
HDOJ/1271_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int n,i,j,k,a,b,c;
int ans[1000],len;
while(~scanf("%d",&n),n)
{
len = 0;
for(i = 1; i<=n; i*=10)
{
c = (n/i)/11;
b = (n/i)%11;
if(b+c && b<10)
{
a = (n-b*i-11*c*i)/2;
if(n == 2*a+b*i+11*c*i)
ans[len++] = a+b*i+c*i*10;
}
b--;
if(b+c && b>=0)
{
a = (n-b*i-11*c*i)/2;
if(n == 2*a+b*i+11*c*i)
ans[len++] = a+b*i+c*i*10;
}
}
if(len)
{
sort(ans,ans+len);
printf("%d",ans[0]);
for(i = 1; i<len; i++)
if(ans[i]!=ans[i-1])
printf(" %d",ans[i]);
printf("\n");
}
else
printf("No solution.\n");
}
return 0;
}

74
HDOJ/1272_autoAC.cpp Normal file
View File

@ -0,0 +1,74 @@
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdlib>
#include<vector>
#include<cmath>
#include<stdlib.h>
#include<iomanip>
#include<list>
#include<deque>
#include<map>
#include <stdio.h>
#include <queue>
#include <stack>
#define maxn 100010
#define ull unsigned long long
#define ll long long
#define reP(i,n) for(i=1;i<=n;i++)
#define rep(i,n) for(i=0;i<n;i++)
#define cle(a) memset(a,0,sizeof(a))
#define mod 90001
#define PI 3.141592657
#define INF 1<<30
const ull inf = 1LL << 61;
const double eps=1e-5;
using namespace std;
bool cmp(int a,int b)
{
return a>b;
}
int fa[maxn];
int ans,cnt;
int mark[maxn];
void init(){
for(int i=0;i<=maxn;i++)
fa[i]=-1;
ans=0;cnt=0;
cle(mark);
}
int findfa(int x){
while(fa[x]>=0)
x=fa[x];
return x;
}
void Union(int x,int y){
int fa_x=findfa(x);
int fa_y=findfa(y);
if(fa_x!=fa_y)
fa[fa_x]=fa_y;
else{
ans++;
}
mark[x]=mark[y]=1;
}
int x,y;
int main()
{
init();
while(cin>>x>>y){
if(x==-1&&y==-1)break;
if(x==0&&y==0){
for(int i=1;i<=maxn;i++){
if((mark[i])&&(fa[i]==-1))
cnt++;
}
if(ans>0||cnt>1)printf("No\n");
else printf("Yes\n");
init();
continue;
}
Union(x,y);
}
return 0;
}

9
HDOJ/1273_autoAC.cpp Normal file
View File

@ -0,0 +1,9 @@
#include<iostream>
using namespace std;
int main()
{
int n;
while(cin>>n&&n)
cout<<(n-1)/2<<endl;
return 0;
}

100
HDOJ/1274_autoAC.cpp Normal file
View File

@ -0,0 +1,100 @@
#include<iostream>
#include<string>
#include<cctype>
#include<stack>
using namespace std;
struct Node{
int k1,k2;
};
Node find(string s){
Node k;
int i,k1,k2;
for(i=s.size()-1;i>=0;i--){
if(s[i]=='('){
k1=i; break;
}
}
if(i==-1) k.k1=0;
else k.k1=k1;
for(i=0;i<s.size();i++){
if(s[i]==')'&&i>k1){
k2=i; break;
}
}
if(i==s.size()) k.k2=0;
else k.k2=k2;
return k;
}
int main()
{
int t;
cin>>t;
while(t--){
string s;
cin>>s;
Node k=find(s);
if(k.k1+k.k2==0){
string s2;
for(int i=0;i<s.size();i++){
if(!i&&islower(s[i])) s2+=s[i];
else if(isdigit(s[i])){
for(int j=0;j<s[i]-'0';j++){
s2+=s[i+1];
}
}
else if(islower(s[i-1])&&islower(s[i])){
s2+=s[i];
}
}
cout<<s2<<endl;
continue;
}
while(k.k1+k.k2){
string s1;
for(int i=0;i<k.k1-1;i++){
s1+=s[i];
}
if(k.k1==0||!isdigit(s[k.k1-1])){
string s2;
if(k.k1==0);
else if(!isdigit(s[k.k1-1])) s2+=s[k.k1-1];
for(int j=0;j<1;j++){
for(int p=k.k1+1;p<k.k2;p++){
if(isdigit(s[p])){
for(int j=0;j<s[p]-'0';j++){
s2+=s[p+1];
}
}
if(!isdigit(s[p-1])&&islower(s[p])){
s2+=s[p];
}
}
s1+=s2;
}
}
else if(isdigit(s[k.k1-1])){
for(int j=0;j<s[k.k1-1]-'0';j++){
string s2;
for(int p=k.k1+1;p<k.k2;p++){
if(isdigit(s[p])){
for(int j=0;j<s[p]-'0';j++){
s2+=s[p+1];
}
}
if(!isdigit(s[p-1])&&islower(s[p])){
s2+=s[p];
}
}
s1+=s2;
}
}
for(int i=k.k2+1;i<s.size();i++){
s1+=s[i];
}
s=s1;
k=find(s);
}
cout<<s<<endl;
}
return 0;
}

35
HDOJ/1275_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<stdio.h>
#include<math.h>
double l,v1,v2,v;
int n;
double f(double t1,double t2)
{
if(t2-t1<=1e-6)
return t1;
double time=(t1+t2)/2;
if(floor((time*v/l+1)/2.0)+floor((time*(v1+v2)/l+1)/2.0)>=n)
return f(t1,time);
else
return f(time,t2);
}
int main()
{
int t;
double time,x,dist;
scanf("%d",&t);
while(t--)
{
scanf("%lf%lf%lf%d",&l,&v1,&v2,&n);
if(v1>v2)
v=v1-v2;
else
v=v2-v1;
time=f(0,100000);
x=time*v1;
while(x>0)
x=x-l;
dist=(-x)<l+x?(-x):l+x;
printf("Time=%.3lf Dist=%.3lf\n",time,dist);
}
return 0;
}

61
HDOJ/1276_autoAC.cpp Normal file
View File

@ -0,0 +1,61 @@
#include<iostream>
#include<string.h>
#include<algorithm>
#include<stdio.h>
using namespace std;
int p[5005];
int main()
{
int m,n,t,k,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&m);
for(i=1;i<=5001;i++)
{
p[i]=1;
}
n=m;
while(n>3)
{
k=0;n=0;
for(i=1;i<=m;i++)
{
if(p[i])
{
k=k+1;
if(k%2==0)
p[i]=0;
n=n+p[i];
}
}
k=0;
if(n<=3)
break;
else
n=0;
for(i=1;i<=m;i++)
{
if(p[i])
{
k=k+1;
if(k%3==0)
p[i]=0;
}
n=n+p[i];
}
}
for(i=1;i<=m;i++)
{
if(p[i])
{
if(i==1)
printf("%d",i);
else
printf(" %d",i);
}
}
printf("\n");
}
return 0;
}

76
HDOJ/1277_autoAC.cpp Normal file
View File

@ -0,0 +1,76 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
struct Tree
{
int end,n,l[10];
void init()
{
memset(l,-1,sizeof(l));
end=n=0;
}
}tr[700000];
int n,m,id;
char s[61000],tt[10000],ss[10005][67];
bool ok[11000];
void add(char*s,int No)
{
int t=0;
for(int i=0;s[i];i++)
{
int now=s[i]-'0';
if(tr[t].l[now]==-1)
{
tr[id].init();
tr[t].l[now]=id++;
}
t=tr[t].l[now];
}
tr[t].end=No;
}
void check(char*s)
{
int t=0;
for(int i=0;s[i];i++)
{
int now=s[i]-'0';
if(tr[t].end&&ok[tr[t].end]==0)
{
printf(" [Key No. %d]",tr[t].end);
ok[tr[t].end]=1;
}
if(tr[t].l[now]==-1) return;
t=tr[t].l[now];
}
}
void init()
{
scanf("%d%d",&n,&m);
int l=0;
for(int i=0;i<n;i++)
{
scanf("%s",tt);
for(int j=0;tt[j];j++) s[l++]=tt[j];
}
s[l]=0;
id=1;
tr[0].init();
for(int i=1;i<=m;i++)
{
scanf("%s",ss[i]);scanf("%s",ss[i]);
scanf("%s",ss[i]);scanf("%s",ss[i]);
add(ss[i],i);
}
printf("Found key:");
for(int i=0;s[i];i++)
{
check(s+i);
}
puts("");
}
int main()
{
init();
return 0;
}

64
HDOJ/1278_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<algorithm>
#include<math.h>
#include<iomanip>
#include<time.h>
#include<string.h>
#include<string>
#include<stack>
#include<vector>
using namespace std;
char map[220][220];
int main(){
int n,m;
while(~scanf("%d%d",&m,&n)){
memset(map,0,sizeof(map));
int *m1,*m2,*n1,*n2,num;
m1=(int*)malloc(sizeof(int)*m);
m2=(int*)malloc(sizeof(int)*m);
n1=(int*)malloc(sizeof(int)*n);
n2=(int*)malloc(sizeof(int)*n);
int i,j,k;
for(i=0;i<m;i++)
scanf("%d",m1+i);
for(i=0;i<m;i++)
scanf("%d",m2+i);
for(i=0;i<n;i++)
scanf("%d",n1+i);
for(i=0;i<n;i++)
scanf("%d",n2+i);
int d[220],h;
for(i=0,h=1;i<n;i++){
for(j=0;j<n2[i];j++)
d[h++]=n1[i];
}
h--;
string l="";
for(i=0;i<m;i++){
for(j=0;j<m1[i];j++)
l+="1";
for(j=0;j<m2[i];j++)
l+="0";
}
for(i=0;i<l.length();i++)
map[l.length()-1-i][0]=l[i];
for(j=1;j<=h;j++){
for(i=0;i<l.length();i++){
map[i][j]=map[(i+d[j]+l.length())%l.length()][j-1];
}
}
string l2="";
for(i=0;i<l.length();i++)
l2+=map[i][h],map[i][h]=0;
reverse(l2.begin(),l2.end());
if(l.compare(l2)==0){
for(i=0;i<l.length();i++)
puts(map[i]);
}
else
printf("Can not make beautilful cloth !\n");
}
return 0;
}

28
HDOJ/1279_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
int main()
{
int N;
int a;
int l;
scanf("%d",&N);
while(N--){
scanf("%d",&a);
l = 0;
while(a>1){
if(a%2){
if(l == 1)
printf(" ");
l = 1;
printf("%d",a);
a = a*3+1;
}
if(a % 2 == 0){
a = a/2;
}
}
if(l == 0)
printf("No number can be output !");
printf("\n");
}
return 0;
}

36
HDOJ/1280_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include<stdio.h>
#include <algorithm>
using namespace std;
#include <string.h>
int a[5005];
int b[5000005];
int main()
{
int i,j,n,m,l,count;
while(scanf("%d%d",&n,&m)!=EOF)
{
l=count=0;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+1+n);
for(i=n;i>=1;i--)
for(j=i-1;j>=0;j--)
{
count++;
b[l++]=a[i]+a[j];
if(count==n*(n-1)/2)
break;
}
sort(b,b+l);
for(i=l-1;m>0;m--,i--)
{
if(m!=1)
printf("%d ",b[i]);
else
printf("%d\n",b[i]);
}
}
return 0;
}

84
HDOJ/1281_autoAC.cpp Normal file
View File

@ -0,0 +1,84 @@
#include<stdio.h>
#include<string.h>
int mark_DFS[10005];
int mark_gx[10005];
int mark_gx2[10005];
int mark_kk[10005];
int tou[10005];
int tot;
typedef struct
{
int date;
int next;
}dian;
dian cun[10005];
void add(int a,int b)
{
tot++;
cun[tot].date=b;
cun[tot].next=tou[a];
tou[a]=tot;
}
int DFS(int x)
{
for(int i=tou[x];i;i=cun[i].next)
{
int too;
too=cun[i].date;
if(mark_DFS[too]||mark_kk[too]==x) continue;
mark_DFS[too]=1;
if(mark_gx[too]==-1||DFS(mark_gx[too]))
{
mark_gx[too]=x;
return 1;
}
}
return 0;
}
int main()
{
int n,a,b,T=1;
while(scanf("%d%d%d",&a,&b,&n)!=EOF)
{
int x,y;
tot=1;
for(int i=0;i<10005;i++)
{
cun[i].next=0;
tou[i]=0;
}
for(int i=1;i<=n;i++)
{
scanf("%d%d",&x,&y);
add(x,y);
}
memset(mark_gx,255,sizeof(mark_gx));
memset(mark_kk,255,sizeof(mark_kk));
memset(mark_gx2,255,sizeof(mark_gx2));
int sum=0;
for(int i=0;i<=a;i++)
{
memset(mark_DFS,0,sizeof(mark_DFS));
sum+=DFS(i);
}
int max=sum;
int num=0;
for(int i=1;i<=a;i++) { mark_gx2[i]=mark_gx[i];}
for(int j=1;j<=a;j++)
if(mark_gx2[j]!=-1)
{
memset(mark_kk,255,sizeof(mark_kk));
mark_kk[j]=mark_gx2[j];
sum=0;
memset(mark_gx,255,sizeof(mark_gx));
for(int i=0;i<=a;i++)
{
memset(mark_DFS,0,sizeof(mark_DFS));
sum+=DFS(i);
}
if(sum<max) num++;
}
printf("Board %d have %d important blanks for %d chessmen.\n",T++,num,max);
}
return 0;
}

34
HDOJ/1282_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<iostream>
#include<vector>
int rev(int );
using namespace std;
int main()
{
int n;
while(cin>>n)
{
vector<int> v;
while(n!=rev(n))
{
v.push_back(n);
n+=rev(n);
}
v.push_back(n);
cout<<v.size()-1<<endl;
int i;
for(i=0;i<v.size()-1;++i)
cout<<v[i]<<"--->";
cout<<v[i]<<endl;
}
return 0;
}
int rev(int n)
{
int sum=0;
while(n!=0)
{
sum=sum*10+n%10;
n/=10;
}
return sum;
}

27
HDOJ/1283_autoAC.cpp Normal file
View File

@ -0,0 +1,27 @@
#include<iostream>
#include<string>
using namespace std;
int main()
{
int m1,m2;
string str;
while(cin>>m1>>m2>>str)
{
int r1,r2,r3;
r1=r2=r3=0;
for(int i=0;i!=str.size();++i)
{
switch(str[i])
{
case 'A': r1=m1;break;
case 'B': r2=m2;break;
case 'C': m1=r3;break;
case 'D': m2=r3;break;
case 'E': r3=r1+r2;break;
case 'F': r3=r1-r2;break;
}
}
cout<<m1<<','<<m2<<endl;
}
return 0;
}

15
HDOJ/1284_autoAC.cpp Normal file
View File

@ -0,0 +1,15 @@
#include<stdio.h>
int main()
{
int n,x2,t,i;
while(~scanf("%d",&n))
{
t=1;
x2=n/2;
t+=x2;
for(i=0;i<=x2;i++)
t+=(n-i*2)/3;
printf("%d\n",t);
}
return 0;
}

60
HDOJ/1285_autoAC.cpp Normal file
View File

@ -0,0 +1,60 @@
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <cstdlib>
int a[501][501];
int b[501],w[501];
int n,m,i,j,s,t;
using namespace std;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(w,0,sizeof(w));
for(i=1;i<=m;i++)
{
cin>>s>>t;
if(!a[s][t])
{
a[s][t]++;
b[t]++;
}
}
int numb=0;
while(1)
{
j=1;
if(b[j]!=0)
{
while(1)
{
j++;
if(b[j]==0)
break;
if(j>n)
break;
}
}
b[j]=-1;
w[numb]=j;
numb++;
for(i=1;i<=n;i++)
{
if(a[j][i]>0)
{
b[i]--;
a[j][i]=-1;
}
}
if(numb>n)
break;
}
for(i=0;i<numb-2;i++)
cout<<w[i]<<' ';
cout<<w[numb-2]<<endl;
}
return 0;
}

26
HDOJ/1286_autoAC.cpp Normal file
View File

@ -0,0 +1,26 @@
#include<iostream>
using namespace std;
const int maxn=32790;
int euler[maxn+2];
void make()
{
euler[1]=0;
for(int i=2;i<=maxn;++i)
euler[i]=i;
for(int i=2;i<=maxn;++i)
if(euler[i]==i)
for(int j=i;j<=maxn;j+=i)
euler[j]=euler[j]/i*(i-1);
}
int main()
{
make();
int n,t,sum;
cin>>t;
while(t--)
{
cin>>n;
cout<<euler[n]<<endl;
}
return 0;
}

43
HDOJ/1287_autoAC.cpp Normal file
View File

@ -0,0 +1,43 @@
#include<iostream>
using namespace std;
char str[10005];
int wen[10005];
int main()
{
int n;
char s;
while(cin>>n)
{
int top=0;
for(int j=0;j<n;j++)
{
cin>>wen[j];
}
for(char c='A';c<='Z';c++)
{
for(int i=0;i<n;i++)
{
s=c^wen[i];
if(s>='A'&&s<='Z')
{
str[top++]=s;
}
else
{
top=0;
break;
}
}
if(top==n)
{
break;
}
}
for(int k=0;k<top;k++)
{
cout<<str[k];
}
cout<<endl;
}
return 0;
}

50
HDOJ/1288_autoAC.cpp Normal file
View File

@ -0,0 +1,50 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <string>
#include <cassert>
#include <ctype.h>
#include <set>
#include <map>
#include <iomanip>
#define INIT(x,y) memset(x,y,sizeof(x))
#define INF 0x7fffffff
#define FAIL {puts("Hat cannot buy tea.");continue;}
using namespace std;
int n, t1, t2, t3, a1, a2, a3;
int main()
{
ios::sync_with_stdio(false);
while (cin >> n >> t1 >> t2 >> t3)
{
if (!n && !t1 && !t2 && !t3) break;
if (t1 + 5 * t2 + 10 * t3 < n) FAIL
if ((t1 == 0) && (t2 == 0) && (n % 10)) FAIL
if ((t1 == 0) && (n % 5)) FAIL
a1 = a2 = a3 = 0;
if (t2)
{
if (t1 < n % 5) FAIL
a1 = n % 5 + min(n - n % 5, (t1 - n % 5) / 5 * 5);
n -= a1;
a2 = (n % 10 == 5) ? 1 : 0;
a2 += min((n - 5 * a2) / 10 * 2, (t2 - a2) / 2 * 2);
n -= a2 * 5;
}
else
{
if (t1 < n % 10) FAIL
a1 = n % 10 + min(n - n % 10, (t1 - n % 10) / 10 * 10);
a2 = 0;
n -= a1;
}
a3 = min(n / 10, t3);
cout << a1 << " YiJiao, " << a2 << " WuJiao, and " << a3 << " ShiJiao" << endl;
}
}

14
HDOJ/1289_autoAC.cpp Normal file
View File

@ -0,0 +1,14 @@
#include <cstdio>
#include <cmath>
#include <iostream>
using namespace std;
int main() {
float x;
while (cin >> x) {
int cnt = 0;
while (fabs(x) >= 2.0) x /= 2.0, cnt++;
while (fabs(x) < 1.0) x *= 2.0, cnt--;
printf("%d %.6f\n", cnt, x);
}
return 0;
}

11
HDOJ/1290_autoAC.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <iostream>
using namespace std;
int n;
int main()
{
while(scanf("%d",&n) != EOF)
{
cout<<(n*n*n + 5*n + 6)/6<<endl;
}
return 0;
}

37
HDOJ/1292_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include<iostream>
#include<string.h>
#include<cmath>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
__int64 a[30][30];
int main()
{
int i,j;
int t,n;
__int64 sum;
memset(a,0,sizeof(a));
a[1][1]=1;
for(i=2;i<26;i++)
{
for(j=1;j<=i;j++)
{
a[i][j]=a[i-1][j-1]+a[i-1][j]*j;
}
}
while(~scanf("%d",&t))
{
while(t--)
{
scanf("%d",&n);
sum=0;
for(i=1;i<=n;i++)
{
sum+=a[n][i];
}
printf("%I64d\n",sum);
}
}
return 0;
}

44
HDOJ/1293_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include<iostream>
using namespace std;
string Da(string a,string b)
{
string ans;
int carry;
int i,j,k;
int add1,add2,sum;
i=a.size()-1;
j=b.size()-1;
k=i>j?i:j;
if(i>j)
ans=a;
else
ans=b;
for(carry=0;k>=0;i--,j--,k--)
{
add1=i<0?0:a[i]-'0';
add2=j<0?0:b[j]-'0';
sum=add2+add1+carry>=10?add1+add2+carry-10:add1+add2+carry;
carry=add1+add2+carry>=10?1:0;
ans[k]=sum+'0';
}
k=ans.size();
if(carry)
ans.insert(0,"1");
return ans;
}
int main()
{
int n,i;
string a[1005];
a[0]="1";
a[1]="3";
for(i=2;i<=1000;i++)
{
a[i]=Da(Da(a[i-1],a[i-1]),a[i-2]);
}
while(cin>>n)
{
cout<<a[n]<<endl;
}
return 0;
}

65
HDOJ/1294_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<algorithm>
#include<cmath>
#include<string>
#include<cstring>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0);
int n;
LL dp[40][40];
LL calc(LL n,int m)
{
LL ans=1;
LL tmp=n+m-1;
for(int i=1;i<=m;i++)
{
ans*=tmp;
ans/=i;
tmp--;
}
return ans;
}
LL dfs(int n,int mx)
{
if(n<mx) return 0;
if(n && !mx) return 0;
if(!n && mx) return 0;
if(!n) return 1;
LL &ans=dp[n][mx];
if(ans!=-1) return ans;
ans=0;
LL tmp=0;
for(int i=0;i<mx;i++)
{
tmp+=dfs(mx-1,i);
}
for(int i=1;i*mx<=n;i++)
{
LL ans1=calc(tmp,i);
LL ans2=0;
for(int j=mx-1;j>=0;j--)
{
ans2+=dfs(n-i*mx,j);
}
ans+=ans1*ans2;
}
return ans;
}
int main()
{
memset(dp,-1,sizeof dp);
while(scanf("%d",&n)==1)
{
LL ans=0;
for(int i=n-1;i>=0;i--)
{
ans+=dfs(n-1,i);
}
printf("%I64d\n",ans);
}
return 0;
}

11
HDOJ/1295_autoAC.cpp Normal file
View File

@ -0,0 +1,11 @@
#include<iostream>
using namespace std ;
int main()
{
int n ;
while(cin >> n)
{
cout << n << endl ;
}
return 0 ;
}

68
HDOJ/1296_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int n;
char a[1005];
while(scanf("%d",&n)!=EOF)
{
scanf("%s",a);
int date=0,poww=0,wei=1,sum=0,cheng=1,flag=0;
for(int i=strlen(a)-1;i>=0;i--)
{
if(a[i]>='0'&&a[i]<='9')
{
date+=(a[i]-'0')*wei;
wei*=10;
poww=0;
}
else if(a[i]=='^')
{
poww=wei=1;
}
else if(a[i]=='X'&&poww)
{
cheng*=pow(n*1.0,date*1.0);
poww=date=0;
wei=1;
}
else if(a[i]=='X')
{
cheng*=n;
poww=0;
wei=1;
}
else if(a[i]=='-')
{
if(date==0)
sum-=cheng;
else sum-=cheng*date;
date=poww=0;
wei=cheng=1;
}
else if(a[i]=='+')
{
if(date==0)
sum+=cheng;
else sum+=date*cheng;
date=poww=0;
wei=cheng=1;
}
if(i==0)
{
if(a[i]=='+'||a[i]=='-') continue;
else flag=1;
}
}
if(flag)
{
if(date==0)
sum+=cheng;
else sum+=date*cheng;
}
printf("%d\n",sum);
}
}

39
HDOJ/1297_autoAC.cpp Normal file
View File

@ -0,0 +1,39 @@
#include<stdio.h>
int a[1001][101];
int i,j;
void add(int n)
{
int k=0;
for(j=1;j<101;j++)
{
k+=a[n-1][j]+a[n-2][j]+a[n-4][j];
a[n][j]=k%10000;
k/=10000;
}
while(k)
{
a[n][j++]=k%10000;
k/=10000;
}
}
int main()
{
int n;
int j=100;
a[1][1]=1;
a[2][1]=2;
a[3][1]=4;
a[4][1]=7;
for(i=5;i<1001;i++)
add(i);
while(scanf("%d",&n)!=EOF)
{
for(i=100;i>0;i--)
if(a[n][i]!=0)break;
printf("%d",a[n][i]);
for(i=i-1;i>0;i--)
printf("%04d",a[n][i]);
printf("\n");
}
return 0;
}

140
HDOJ/1298_autoAC.cpp Normal file
View File

@ -0,0 +1,140 @@
#include<iostream>
#include<algorithm>
#include<string>
#include<cstdio>
using namespace std;
struct node
{
int count;
struct node*next[26];
int temp;
};
struct node*newnode()
{
int i;
struct node *r;
r=new struct node;
for(i=0;i<26;i++)
r->next[i]=NULL;
r->count=0;
r->temp=0;
return r;
}
void build(struct node *root,string str,int v)
{
struct node*r,*s;
int i;
r=root;
for(i=0;i<str.length();i++)
{
if(r->next[str[i]-'a']==NULL)
{
s=newnode();
s->count=v;
r->next[str[i]-'a']=s;
r=s;
}
else
{
r=r->next[str[i]-'a'];
r->count+=v;
}
}
r->temp=1;
}
char tel[8][5]={"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
struct pa
{
char c;
int sum;
};
int cmp(struct pa a,struct pa b)
{
if(a.sum==b.sum)
return a.c<b.c;
return a.sum>b.sum;
}
char ans[105]="";
char temp[105]="";
int flag;
char sum[105];
void search(struct node*root,string str,int v)
{
int i;
struct node *r;
r=root;
if(v==str.length())
{
if(r->count>flag)
{
flag=r->count;
sum[v]='\0';
strcpy(ans,sum);
}
return ;
}
int t=0;
struct pa a[4];
int L=strlen(tel[str[v]-'2']);
int k=0;
for(i=0;i<L;i++)
{
if(r->next[tel[str[v]-'2'][i]-'a']!=NULL)
{
a[k].sum=r->next[tel[str[v]-'2'][i]-'a']->count;
a[k].c=tel[str[v]-'2'][i];
k++;
}
}
sort(a,a+k,cmp);
for(i=0;i<k;i++)
{
sum[v]=a[i].c;
search(root->next[a[i].c-'a'],str,v+1);
}
return ;
}
int main()
{
int T;
cin>>T;
for(int w=1;w<=T;w++)
{
printf("Scenario #%d:\n",w);
int N;
char p[105];
struct node*root;
root=newnode();
cin>>N;
while(N--)
{
int v;
scanf("%s%d",p,&v);
build(root,p,v);
}
int M;int L;
cin>>M;
int tag=0;
while(M--)
{
scanf("%s",p);
L=strlen(p);
char q[105];
int i;
for(i=1;i<L;i++)
{
strcpy(q,p);
q[i]='\0';
flag=0;
search(root,q,0);
if(!flag)
printf("MANUALLY\n");
else
puts(ans);
}
printf("\n");
}
printf("\n");
}
return 0;
}

51
HDOJ/1299_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include <iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
int is_prime[100000];
int prime[50000];
void prepare()
{
memset(is_prime,1,sizeof(is_prime));
for(int i=2;i<1000;i++)
{
if(is_prime[i])
for(int j=i;i*j<100000;j++)
is_prime[i*j]=0;
}
int s=1;
for(int i=2;i<100000;i++)
if(is_prime[i])
prime[s++]=i;
}
int main()
{
int t;
scanf("%d",&t);
prepare();
int n;
int temp=0;
while(t--)
{
scanf("%d",&n);
int s=1,ans=1;;
while(prime[s]*prime[s]<=n)
{
if(n%prime[s]==0)
{
int coun=0;
while(n%prime[s]==0)
{
coun++;
n=n/prime[s];
}
ans*=(coun*2+1);
}
s++;
}
if(n!=1)
ans*=3;
printf("Scenario #%d:\n%d\n\n",++temp,(ans+1)/2);
}
return 0;
}