Powered By HC TECH : AutoACer Engine

1600-1699
This commit is contained in:
KiritoTRw 2016-08-15 09:13:31 +08:00 committed by GitHub
parent ed2de2e423
commit 2d9cf4fdf2
57 changed files with 3986 additions and 0 deletions

168
HDOJ/1603_autoAC.cpp Normal file
View File

@ -0,0 +1,168 @@
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<queue>
#include<map>
#include<vector>
using namespace std;
#define N 1005*1005
#define RN 1005
#define M 1005
struct DLX
{
int n,m,C;
int U[N],D[N],L[N],R[N],Row[N],Col[N];
int H[M],S[M],cnt,ans[M];
void init(int _n,int _m)
{
n=_n;
m=_m;
for(int i=0; i<=m; i++)
{
U[i]=D[i]=i;
L[i]=(i==0?m:i-1);
R[i]=(i==m?0:i+1);
S[i]=0;
}
C=m;
for(int i=1; i<=n; i++) H[i]=-1;
}
void link(int x,int y)
{
C++;
Row[C]=x;
Col[C]=y;
S[y]++;
U[C]=U[y];
D[C]=y;
D[U[y]]=C;
U[y]=C;
if(H[x]==-1) H[x]=L[C]=R[C]=C;
else
{
L[C]=L[H[x]];
R[C]=H[x];
R[L[H[x]]]=C;
L[H[x]]=C;
}
}
void del(int x)
{
R[L[x]]=R[x];
L[R[x]]=L[x];
for(int i=D[x]; i!=x; i=D[i])
{
for(int j=R[i]; j!=i; j=R[j])
{
U[D[j]]=U[j];
D[U[j]]=D[j];
S[Col[j]]--;
}
}
}
void rec(int x)
{
for(int i=U[x]; i!=x; i=U[i])
{
for(int j=L[i]; j!=i; j=L[j])
{
U[D[j]]=j;
D[U[j]]=j;
S[Col[j]]++;
}
}
R[L[x]]=x;
L[R[x]]=x;
}
int dance(int x)
{
if(R[0]==0 || R[0]>16)
{
cnt=x;
return 1;
}
int now=R[0];
for(int i=R[0]; i!=0 && i<=16; i=R[i])
{
if(S[i]<S[now]) now=i;
}
del(now);
for(int i=D[now]; i!=now; i=D[i])
{
ans[x]=Row[i];
for(int j=R[i]; j!=i; j=R[j]) del(Col[j]);
if(dance(x+1)) return 1;
for(int j=L[i]; j!=i; j=L[j]) rec(Col[j]);
}
rec(now);
return 0;
}
} dlx;
struct node
{
int m,id;
int w[44];
} kx[1234];
int main()
{
int n;
while(scanf("%d",&n),n)
{
int cnt=0;
dlx.init(n*16,16+n);
for(int i=1; i<=n; i++)
{
int a,b;
scanf("%d%d",&a,&b);
char v[12][12];
for(int j=0; j<a; j++) scanf("%s",v[j]);
for(int xx=1; xx+a<=5; xx++)
{
for(int yy=1; yy+b<=5; yy++)
{
cnt++;
kx[cnt].m=0;
kx[cnt].id=i;
for(int x=0; x<a; x++)
{
for(int y=0; y<b; y++)
{
if(v[x][y]=='1')
{
int tep=(xx+x-1)*4+(yy+y);
dlx.link(cnt,tep);
kx[cnt].w[kx[cnt].m++]=tep;
}
}
}
dlx.link(cnt,16+i);
}
}
}
int f=dlx.dance(0);
if(f==0) puts("No solution possible");
else
{
char mp[10][10];
for(int i=0;i<dlx.cnt;i++)
{
for(int j=0;j<kx[dlx.ans[i]].m;j++)
{
int x,y;
x=(kx[dlx.ans[i]].w[j]-1)/4;
y=kx[dlx.ans[i]].w[j]%4-1;
if(y==-1) y=3;
mp[x][y]=kx[dlx.ans[i]].id+'0';
}
}
for(int i=0;i<4;i++)
{
mp[i][4]='\0';
puts(mp[i]);
}
}
puts("");
}
return 0;
}

69
HDOJ/1606_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <string.h>
#include <algorithm>
#include <map>
using namespace std;
struct p{
char s[100];
int num;
}a[100];
bool cmp(p a,p b)
{
return a.num>b.num;
}
int main()
{
int n,m,i,j,z=1;
char b[100];
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(a,0,sizeof(a));
getchar();
map<string,int>q;
for(i=0;i<n;i++)
{
scanf("%s",b);
q[b]=1;
}
getchar();
for(i=0;i<m;i++)
{
gets(a[i].s);
int t=strlen(a[i].s);
string w="";
char x[100];
strcpy(x,a[i].s);
for(j=0;j<t;j++)
{
if(x[j]>=65&&x[j]<=90)
x[j]+=32;
}
for(j=0;j<t;j++)
{
if(x[j]>=97&&x[j]<=122)
w=w+x[j];
else
{
map<string,int>::iterator it;
it=q.find(w);
if(it!=q.end())
a[i].num++;
w="";
}
}
}
sort(a,a+m,cmp);
int v=a[0].num;
printf("Excuse Set #%d\n",z);
z++;
for(i=0;i<m;i++)
{
if(a[i].num==v)
printf("%s\n",a[i].s);
else
break;
}
printf("\n");
}
return 0;
}

106
HDOJ/1608_autoAC.cpp Normal file
View File

@ -0,0 +1,106 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
int x[39][39];
int n;
int dx[] = {1,0,-1,0};
int dy[] = {0,1,0,-1};
struct node
{
int x,y,len,dir;
int move;
} re[19];
bool oor(int x,int y)
{
if(x<0||x>29) return false;
if(y<0||y>29) return false;
return true;
}
bool stop[19],cash[19];
int map_move[39][39];
void init()
{
int l,r,c;
char d[5];
memset(map_move,-1,sizeof(map_move));
memset(x,false,sizeof(x));
for(int i=0; i<n; i++)
{
scanf("%s%d%d%d",d,&l,&r,&c);
re[i].len = l,re[i].x = r,re[i].y = c;
if(d[0]=='R') re[i].dir = 0;
else if(d[0]=='U') re[i].dir = 1;
else if(d[0]=='L') re[i].dir = 2;
else re[i].dir = 3;
re[i].move = 1;
int tx = r,ty = c;
for(int j =0;j<l;j++)
{
map_move[tx][ty] = i;
tx-=dx[re[i].dir];
ty-=dy[re[i].dir];
}
}
}
void solve()
{
bool fig = true;
while(fig)
{
fig = false;
for(int i=0; i<n; i++)
{
if(re[i].move==0)
{
}
else
{
fig = true;
re[i].x+=dx[re[i].dir];
re[i].y+=dy[re[i].dir];
if(!oor(re[i].x,re[i].y))
{
if(re[i].len>0)
{
re[i].x-=dx[re[i].dir];
re[i].y-=dy[re[i].dir];
re[i].len--;
if(re[i].len<=0) re[i].move = 0;
}
}
if(map_move[re[i].x][re[i].y]!=-1&&map_move[re[i].x][re[i].y]!=i)
{
re[map_move[re[i].x][re[i].y]].move = 0;
re[i].move = 0;
x[re[i].x][re[i].y] = true;
}
map_move[re[i].x][re[i].y]=i;
map_move[re[i].x-dx[re[i].dir]*(re[i].len)][re[i].y-dy[re[i].dir]*(re[i].len)]=-1;
}
}
}
cout<<" 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2"<<endl;
cout<<" 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9"<<endl;
for(int i=0; i<30; i++)
{
printf("%02d",29-i);
for(int j=0; j<30; j++)
if(!x[j][29-i]) cout<<" .";
else cout<<" X";
cout<<endl;
}
cout<<endl;
}
int main()
{
while(~scanf("%d",&n))
{
init();
solve();
}
return 0;
}

87
HDOJ/1609_autoAC.cpp Normal file
View File

@ -0,0 +1,87 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 15;
const int M = 105;
int t, p, c, m, vis[M], have[M], hn, b[M], s[M], ans[N], num;
struct Mi {
int num[N];
int vis[M];
int b, w;
} mi[M];
void init() {
int i, j;
memset(vis, 0, sizeof(vis));
memset(b, 0, sizeof(b));
memset(s, 0, sizeof(s));
hn = 0;
scanf("%d%d%d", &p, &c, &m);
for (i = 0; i < m; i++) {
memset(mi[i].vis, 0, sizeof(mi[i].vis));
for (j = 0; j < p; j++) {
scanf("%d", &mi[i].num[j]);
mi[i].vis[mi[i].num[j]]++;
if (!vis[mi[i].num[j]]) {
vis[mi[i].num[j]] = 1;
have[hn++] = mi[i].num[j];
}
}
scanf("%d%d", &mi[i].b, &mi[i].w);
}
for (i = 1; i <= c; i++)
if (!vis[i]) {
have[hn++] = i;
break;
}
sort(have, have + hn);
}
bool dfs(int d) {
if (d == p) {
for (int i = 0; i < m; i++) {
if (b[i] != mi[i].b || s[i] - b[i] != mi[i].w)
return false;
}
for (int j = 0; j < p - 1; j++)
printf("%d ", ans[j]);
printf("%d\n", ans[p - 1]);
return true;
}
for (int i = 0; i < hn; i++) {
int num = have[i], flag = 0, j, flag2[M];
memset(flag2, 0, sizeof(flag2));
for (j = 0; j < m; j++) {
if (num == mi[j].num[d])
b[j]++;
if (mi[j].vis[num]) {
s[j]++;
mi[j].vis[num]--;
flag2[j] = 1;
}
if (b[j] > mi[j].b || s[j] > mi[j].b + mi[j].w)
flag = 1;
}
if (!flag) {
ans[d] = num;
if (dfs(d + 1)) return true;
}
for (j = 0; j < m; j++) {
if (num == mi[j].num[d])
b[j]--;
if (flag2[j]) {
s[j]--;
mi[j].vis[num]++;
}
}
}
return false;
}
int main() {
scanf("%d", &t);
while (t--) {
init();
if (!dfs(0)) printf("You are cheating!\n");
}
return 0;
}

44
HDOJ/1611_autoAC.cpp Normal file
View File

@ -0,0 +1,44 @@
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std ;
int cost[200][200],price[200][200],indexx[200][200] ;
void print(int K,int L,char key[],char let[])
{
if( K == 0 ) return ;
print(K-1, L-indexx[K][L],key,let) ;
printf("%c: ",key[K-1]) ;
for(int i = L-indexx[K][L] ; i < L ; i++)
putchar(let[i]) ;
puts("") ;
}
int main()
{
int T ;
scanf("%d",&T) ;
int K,L ,a[200] ,cnt = 0;
while(T--)
{
char key[200],let[200] ;
cnt++ ;
scanf("%d %d %s %s",&K,&L,key,let) ;
for(int i = 0 ; i < L ; i++)
scanf("%d",&a[i]) ;
memset(price,0x40,sizeof(price)) ;
price[0][0] = 0 ;
for(int i = 1 ; i <= L ; i++)
for(int j = i ; j <= L ; j++)
cost[i][j] = cost[i][j-1]+(j-i+1)*a[j-1] ;
for(int i = 1 ; i <= K ; i++)
for(int j = i ; j <= L ; j++)
for(int k = 1 ; k <= j-i+1 ; k++)
{
if(price[i-1][j-k] + cost[j-k+1][j] <= price[i][j])
price[i][j] = price[i-1][j-k] + cost[j-k+1][j],indexx[i][j] = k ;
}
printf("Keypad #%d:\n",cnt) ;
print(K,L,key,let) ;
printf("\n") ;
}
return 0 ;
}

32
HDOJ/1613_autoAC.cpp Normal file
View File

@ -0,0 +1,32 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=6;
const int inf=1<<29;
int sum[maxn],val[maxn][maxn];
char str[maxn][10]={"BCG","BGC","CBG","CGB","GBC","GCB"};
int main()
{
while(scanf("%d%d%d",&val[0][0],&val[0][1],&val[0][2])!=EOF)
{
for(int i=1;i<=2;i++)
for(int j=0;j<3;j++)
scanf("%d",&val[i][j]);
sum[0]=val[1][0]+val[2][0]+val[0][2]+val[2][2]+val[0][1]+val[1][1];
sum[1]=val[1][0]+val[2][0]+val[0][1]+val[2][1]+val[0][2]+val[1][2];
sum[2]=val[1][2]+val[2][2]+val[0][0]+val[2][0]+val[0][1]+val[1][1];
sum[3]=val[1][2]+val[2][2]+val[0][1]+val[2][1]+val[0][0]+val[1][0];
sum[4]=val[1][1]+val[2][1]+val[0][0]+val[2][0]+val[0][2]+val[1][2];
sum[5]=val[1][1]+val[2][1]+val[0][2]+val[2][2]+val[0][0]+val[1][0];
int ans=inf,pos;
for(int i=0;i<maxn;i++)
if(ans>sum[i])
{
ans=sum[i];
pos=i;
}
printf("%s %d\n",str[pos],ans);
}
return 0;
}

71
HDOJ/1614_autoAC.cpp Normal file
View File

@ -0,0 +1,71 @@
#include <algorithm>
#include <memory.h>
#include <cstdio>
#include <vector>
using namespace std;
const int MAX = 31;
struct Box{
int dims[11];
}boxes[MAX];
bool g[MAX][MAX];
int dp[MAX], fa[MAX], n, k;
//check if box a can be nested into box b
bool can_be_nested(Box & a, Box & b, int len){
for(int i = 0; i < len; ++i){
if(a.dims[i] >= b.dims[i])return false;
}
return true;
}
int memoized_dfs(int i){
if(dp[i] != 0)return dp[i];
int max_len = 0;
for(int j = 1; j <= k; ++j){
if(g[i][j]){
int next_len = memoized_dfs(j);
if(next_len > max_len){
max_len = next_len;
fa[i] = j;
}
}
}
return dp[i] = max_len + 1;
}
int main(int argc, char const *argv[]){
while(scanf("%d %d", &k, &n) == 2){
memset(g, false, sizeof(g));
for(int i = 1; i <= k; ++i){
for(int j = 0; j < n; ++j){
scanf("%d", &boxes[i].dims[j]);
}
sort(boxes[i].dims, boxes[i].dims + n);
}
for(int i = 1; i <= k; ++i){
for(int j = 1; j <= k; ++j){
if(can_be_nested(boxes[j], boxes[i], n)){
g[i][j] = true;
}
}
}
vector<int> ans;
for(int i = 1; i <= k; ++i){
memset(dp, 0, sizeof(dp));
memset(fa, 0, sizeof(fa));
int length = memoized_dfs(i);
if(length > ans.size()){
ans.clear();
int p = i;
while(p != 0){
ans.push_back(p);
p = fa[p];
}
}
}
printf("%d\n", ans.size());
for(int i = ans.size() - 1; i >= 0; --i){
if(i < ans.size() - 1)printf(" ");
printf("%d", ans[i]);
}
printf(" \n");
}
return 0;
}

29
HDOJ/1615_autoAC.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
#include <math.h>
using namespace std;
int main(void) {
for (int i, m, n, nMaxM, nMaxN, nCnt = 0; cin >> i; nCnt = 0) {
bool aFlags[1000001] = {false};
for (m = 2, nMaxM = (int)sqrt((float)i - 1); m <= nMaxM; ++m) {
nMaxN = (int)sqrt((float)i - m * m);
nMaxN = nMaxN >= m ? m - 1 : nMaxN;
for (n = 1; n <= nMaxN; ++n) {
if (n % 2 != m % 2) {
int a = m, b = n, c;
for(int r; (r = a % b) != 0; a = b, b = r);
if (b == 1) {
++nCnt;
a = m * m - n * n, b = 2 * m * n, c = m * m + n * n;
for (int k = 0; c * k <= i; ++k) {
aFlags[a * k] = aFlags[b * k] = aFlags[c * k] = 1;
}
}
}
}
}
cout << nCnt << ' ';
for (nCnt = 0, m = 1; m <= i; nCnt += !aFlags[m++]);
cout << nCnt << endl;
}
return 0;
}

104
HDOJ/1616_autoAC.cpp Normal file
View File

@ -0,0 +1,104 @@
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int num,top,ii;
struct node
{
int x;
int y;
}a[105],mis[50005];
struct
{
node b[25];
int to;
}an[25];
int cmp(const void *c,const void *d)
{
int ans;
struct node *p1=(node *)c;
struct node *p2=(node *)d;
ans=(p1->x-a[0].x)*(p2->y-a[0].y)-(p2->x-a[0].x)*(p1->y-a[0].y);
if (ans>0) return -1;
else if (ans<0) return 1;
else
{
if (abs(a[0].x-p1->x)<abs(a[0].x-p2->x)) return 1;
return -1;
}
}
int find( int p2,int p1,int p0)
{
return (an[ii].b[p1].x-an[ii].b[p0].x)*(a[p2].y-an[ii].b[p0].y)-(a[p2].x-an[ii].b[p0].x)*(an[ii].b[p1].y-an[ii].b[p0].y);
}
int find2( node p2,node p1,node p0)
{
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
}
void Graham(int n)
{
int i;
a[n]=a[num];
a[num]=a[0];
a[0]=a[n];
qsort(a+1,n-1,sizeof(a[0]),cmp);
an[ii].b[0]=a[0];
an[ii].b[1]=a[1];
top=1;
for (i=2;i<n;i++)
{
while (find(i,top,top-1)<0) top--;
an[ii].b[++top]=a[i];
}
}
int main ()
{
int n,i,j,k,h,minx=10005,miny=10005,dis,max,misnum,sum;
double area;
ii=0;
while(scanf("%d",&n),n!=-1)
{
minx=10005;miny=10005;
for (i=0;i<n;i++)
{
scanf("%d %d",&a[i].x,&a[i].y);
if (a[i].y<miny || (miny==a[i].y && a[i].x<minx))
{
miny=a[i].y;minx=a[i].x;num=i;
}
}
Graham(n);
an[ii].to=top;
ii++;
}
i=0;
while(scanf("%d%d",&mis[i].x,&mis[i].y)!=EOF)
i++;
misnum=i;area=0;
for(i=0;i<ii;i++)
{
for(j=0;j<misnum;j++)
{
for(k=0;k<=an[i].to;k++)
{
if(k==an[i].to)
{
if(find2(mis[j],an[i].b[0],an[i].b[k])<0)
break;
}
else if(find2(mis[j],an[i].b[k+1],an[i].b[k])<0)
break;
}
if(k==an[i].to+1)
{
sum=0;
for(h=0;h<an[i].to;h++)
sum+=an[i].b[h].x*an[i].b[h+1].y-an[i].b[h].y*an[i].b[h+1].x;
sum+=an[i].b[h].x*an[i].b[0].y-an[i].b[h].y*an[i].b[0].x;
area+=sum*1.0/2;
break;
}
}
}
printf("%.2f\n",area);
return 0;
}

62
HDOJ/1617_autoAC.cpp Normal file
View File

@ -0,0 +1,62 @@
#include<iostream>
void writesel(int n,int max,char now[])
{
char thenew='a'+n-1;int space,i,j;
char * p;
for(i=0;i<n;i++)
{
if(n!=1)
{
for(space=0;space<2*(n-1);space++)std::cout<<" ";
if(i==0)std::cout<<"if "<<now[n-2]<<" < "<<thenew<<" then"<<std::endl;
else if(i==n-1)std::cout<<"else"<<std::endl;
else std::cout<<"else if "<<now[n-2-i]<<" < "<<thenew<<" then"<<std::endl;
}
p=new char[n];
for(j=0;j<n;j++)
{
if(j==n-i-1)p[j]=thenew;
else if(j<n-i-1)p[j]=now[j];
else p[j]=now[j-1];
}
if(n==max)
{
for(space=0;space<2*(n-1);space++)std::cout<<" ";
std::cout<<" writeln(";
for(j=0;j<n;j++)
{
std::cout<<p[j];
if((n-j)!=1)std::cout<<',';
}
std::cout<<")"<<std::endl;
}
else writesel(n+1,max,p);
}
}
int main()
{
int n,i;
std::cin>>n;
char *p=new char[n];
std::cout<<"program sort(input,output);"<<std::endl;
std::cout<<"var"<<std::endl;
for(i=0;i<n;i++)
{
p[i]='a'+i;
std::cout<<p[i];
if((n-i)!=1)std::cout<<',';
}
std::cout<<" : integer;"<<std::endl;
std::cout<<"begin"<<std::endl;
std::cout<<" readln(";
for(i=0;i<n;i++)
{
p[i]='a'+i;
std::cout<<p[i];
if((n-i)!=1)std::cout<<',';
}
std::cout<<");"<<std::endl;
writesel(1,n,"");
std::cout<<"end."<<std::endl;
return 0;
}

69
HDOJ/1619_autoAC.cpp Normal file
View File

@ -0,0 +1,69 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int NM=205;
const int MAX=0xfffffff;
int d[3][2]={-1,1,0,1,1,1};
int a[NM][NM],dp[NM][NM],path[NM],c[3],n,m,k;
bool flag;
inline int min(int x,int y){
return x<y?x:y;
}
int DFS(int x,int y)
{
int x1,y1,i;
if(dp[x][y]!=MAX) return dp[x][y];
if(y==m) return dp[x][y]=a[x][y];
for(i=0;i<3;i++){
x1=x+d[i][0];y1=y+d[i][1];
if(!x1) x1=n;if(x1>n) x1=1;
dp[x][y]=min(dp[x][y],DFS(x1,y1));
}
return dp[x][y]=dp[x][y]+a[x][y];
}
void Findp(int x,int y)
{
int x1,y1,i;
if(flag) return;
path[k++]=x;
if(y==m){
flag=true;return;
}
for(i=0;i<3;i++){
x1=x+d[i][0];
if(!x1) x1=n;if(x1>n) x1=1;
c[i]=x1;
}
sort(c,c+3);
for(i=0;i<3;i++){
y1=y+d[i][1];
if(dp[x][y]==dp[c[i]][y1]+a[x][y])
Findp(c[i],y1);
}
}
int main()
{
int i,j,u,mmin;
while(~scanf("%d%d",&n,&m)){
for(i=1;i<=n;i++)
for(j=1;j<=m;j++){
scanf("%d",&a[i][j]);
dp[i][j]=MAX;
}
u=1;mmin=MAX;
for(i=1;i<=n;i++){
if(mmin>DFS(i,1)){
mmin=dp[i][1],u=i;
}
}
k=0;flag=false;
memset(path,0,sizeof(path));
Findp(u,1);
for(i=0;i<k-1;i++) printf("%d ",path[i]);
printf("%d\n",path[k-1]);
printf("%d\n",mmin);
}
return 0;
}

75
HDOJ/1620_autoAC.cpp Normal file
View File

@ -0,0 +1,75 @@
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
#define MAXN 55
#define DANGEROUS 0
#define SAFE 1
struct status
{
int x, y;
string direction;
};
int grid[MAXN][MAXN], width, height;
bool move(status & s, string instructions)
{
status last;
for (int i = 0; i < instructions.length(); i++)
{
last = s;
if (s.direction == "N" && instructions[i] == 'L')
s.direction = "W";
else if (s.direction == "N" && instructions[i] == 'R')
s.direction = "E";
else if (s.direction == "N" && instructions[i] == 'F')
s.y += 1;
else if (s.direction == "S" && instructions[i] == 'L')
s.direction = "E";
else if (s.direction == "S" && instructions[i] == 'R')
s.direction = "W";
else if (s.direction == "S" && instructions[i] == 'F')
s.y -= 1;
else if (s.direction == "W" && instructions[i] == 'L')
s.direction = "S";
else if (s.direction == "W" && instructions[i] == 'R')
s.direction = "N";
else if (s.direction == "W" && instructions[i] == 'F')
s.x -= 1;
else if (s.direction == "E" && instructions[i] == 'L')
s.direction = "N";
else if (s.direction == "E" && instructions[i] == 'R')
s.direction = "S";
else if (s.direction == "E" && instructions[i] == 'F')
s.x += 1;
if (s.x > width || s.x < 0 || s.y > height || s.y < 0)
{
if (grid[last.y][last.x] == DANGEROUS)
s = last;
else
{
cout << last.x << " " << last.y << " " << last.
direction;
cout << " LOST" << endl;
grid[last.y][last.x] = DANGEROUS;
return true;
}
}
}
cout << s.x << " " << s.y << " " << s.direction << endl;
return false;
}
int main(int argc, char const *argv[])
{
status robot;
string line;
cin >> width >> height;
for (int i = 0; i <= height; i++)
for (int j = 0; j <= width; j++)
grid[i][j] = SAFE;
while (cin >> robot.x >> robot.y >> robot.direction)
{
cin >> line;
move(robot, line);
}
return 0;
}

34
HDOJ/1621_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int xiede(double m,double n)
{
double gen3=1.732051;
int max,x,y;
x=(int)(2*gen3*(m-1)/3.0);
if(m>=1)x++;
y=(int)n;
max=x*y;
if(n-y<0.50000)
max=max-(int)(x/2);
return max;
}
int main()
{
double m,n,gen3=1.732051;
int aa,bb,max1,max2,max3,max23,x,y;
while(~scanf("%lf%lf",&m,&n))
{
aa=(int)m;bb=(int)n;
max1=aa*bb;
max2=xiede(m,n);
max3=xiede(n,m);
max23=max2>=max3?max2:max3;
if(max1>=max23)
printf("%d grid\n",max1);
else
printf("%d skew\n",max23);
}
return 0;
}

88
HDOJ/1622_autoAC.cpp Normal file
View File

@ -0,0 +1,88 @@
#include<algorithm>
#include<iostream>
#include<string.h>
#include<cstdlib>
#include<cstdio>
#include<vector>
#include<queue>
using std::queue;
using std::vector;
const int Max_N = 260;
struct Node {
int v, vis;
Node *ch[2];
inline void set(int _v, Node *p) {
vis = 0, v = _v;
ch[0] = ch[1] = p;
}
};
struct BinTree {
int fail;
char buf[Max_N];
Node *tail, *root, stack[Max_N];
void init() {
fail = 0;
tail = &stack[0];
}
inline Node *newNode(int v = 0) {
Node *p = tail++;
p->set(v, NULL);
return p;
}
inline void insert(const char *src, const int v) {
int n = strlen(src);
Node *x = root;
for (int i = 0; i < n; i++) {
if (src[i] == 'L') {
if (!x->ch[0]) x->ch[0] = newNode();
x = x->ch[0];
} else if (src[i] == 'R') {
if (!x->ch[1]) x->ch[1] = newNode();
x = x->ch[1];
}
}
if (x->vis) fail = 1;
x->v = v;
x->vis = 1;
}
inline void bfs() {
vector<int> ans;
queue<Node *> que;
que.push(root);
while (!que.empty()) {
Node *u = que.front(); que.pop();
if (!u->vis) {
fail = 1;
break;
}
ans.push_back(u->v);
if (u->ch[0]) que.push(u->ch[0]);
if (u->ch[1]) que.push(u->ch[1]);
}
if (fail) {
puts("not complete");
return;
}
int n = ans.size();
for (int i = 0; i < n; i++) {
printf("%d%c", ans[i], i < n - 1 ? ' ' : '\n');
}
}
inline int gogo() {
init();
int v = 0;
root = newNode();
for (;;) {
if (scanf("%s", buf) != 1) return 0;
if (!strcmp(buf, "()")) break;
sscanf(&buf[1], "%d", &v);
insert(strchr(buf, ',') + 1, v);
}
bfs();
return 1;
}
}tree;
int main() {
while (tree.gogo());
return 0;
}

111
HDOJ/1623_autoAC.cpp Normal file
View File

@ -0,0 +1,111 @@
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
#define MAXN 400
char title[204][MAXN];
char keyword[52][14];
typedef struct charter{
char character[MAXN];
int rank;
int start, end;
}charter;
charter mes[3004];
int dealenter(char *temp)
{
int len = strlen(temp);
if(temp[len-1] == '\n') temp[len-1] = ' ';
else temp[len++] = ' ', temp[len] = '\0';
return 0;
}
int calculate(int n1, int n2, int n3)
{
int i, j, k, flag;
charter temp;
for(i=0; i<n3-1; ++i)
{
flag = 1;
for(j=0; j<n3-1-i; ++j)
{
if(strcmp(mes[j].character, mes[j+1].character) > 0)
{
flag = 0;
temp = mes[j], mes[j] = mes[j+1], mes[j+1] = temp;
}
}
if(flag) break;
}
return 0;
}
int shuai(int n1, char *from)
{
int i, j, flag = 0;
for(i=0; i<n1; ++i)
{
if(strcmp(keyword[i], from) == 0) {flag = 1; break;}
}
return flag;
}
int lastline(int n1, int n2, int n3)
{
int i, j, k, t, flag, len, start, end;
for(i=0; i<n3; ++i)
{
if(!shuai(n1, mes[i].character))
{
j = mes[i].rank;
start = mes[i].start, end = mes[i].end;
for(t=0; t<start; ++t)
printf("%c", title[j][t]);
len = strlen(mes[i].character);
for(k=0; k<len; ++k)
printf("%c", mes[i].character[k] - ('a' - 'A'));
len = strlen(title[j]);
for(k=end+1; k<len; ++k)
printf("%c", title[j][k]);
printf("\n");
}
}
}
int main()
{
int i, j, t, k, n, m, count, cnt3, cnt1, cnt2, cnt4, flag, flag2, len, doc;
char temp[MAXN];
flag = cnt1 = cnt2 = cnt3 = 0;
while(fgets(temp, MAXN, stdin) != NULL)
{
dealenter(temp);
len = strlen(temp);
if(strcmp(temp, ":: ") == 0) flag = 1;
else if(!flag)
{
strcpy(keyword[cnt1], temp);
keyword[cnt1++][len-1] = '\0';
}
else
{
for(i=0,flag2=cnt4=0,doc=-1; i<len; ++i)
{
if(temp[i] != ' ')
{
if(doc == -1) doc = i;
if(isupper(temp[i])) mes[cnt3].character[cnt4++] = temp[i] = towlower(temp[i]);
else mes[cnt3].character[cnt4++] = temp[i];
flag2 = 1;
}
else if(flag2)
{
mes[cnt3].rank = cnt2, mes[cnt3].start = doc, mes[cnt3].end = i-1;
mes[cnt3].character[cnt4] = '\0';
cnt3++, cnt4 = 0;
flag2 = 0, doc = -1;
}
}
strcpy(title[cnt2], temp);
title[cnt2++][len-1] = '\0';
}
}
calculate(cnt1, cnt2, cnt3);
lastline(cnt1, cnt2, cnt3);
return 0;
}

47
HDOJ/1625_autoAC.cpp Normal file
View File

@ -0,0 +1,47 @@
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
#define MAXN 55
int path[MAXN][MAXN];
int m,n;
void floyd(){
for(int k=0;k<=n;k++){
for(int i=0;i<=n;i++){
for(int j=0;j<=n;j++){
path[i][j]+=path[i][k]*path[k][j];
}
}
}
for(int i=0;i<=n;i++)if(path[i][i]){
path[i][i]=-1;
for(int j=0;j<=n;j++){
for(int k=0;k<=n;k++){
if(path[j][i]&&path[i][k]){
path[j][k]=-1;
}
}
}
}
}
int main(){
int u,v,t=0;
while(~scanf("%d",&m)){
memset(path,0,sizeof(path));
n=0;
for(int i=1;i<=m;i++){
scanf("%d%d",&u,&v);
path[u][v]=1;
n=max(n,max(u,v));
}
printf("matrix for city %d\n",t++);
floyd();
for(int i=0;i<=n;i++){
for(int j=0;j<n;j++){
printf(" %d",path[i][j]);
}
printf(" %d\n",path[i][n]);
}
}
return 0;
}

53
HDOJ/1626_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<iostream>
#include<cstring>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<cmath>
#include<map>
#include<queue>
using namespace std;
const int radix=256*256;
int main()
{
string s;
long long r;
while(getline(cin,s))
{
if(s[0]=='#')break;
if(s.length()==0)
{
cout<<"00 00"<<endl;
continue;
}
int len=s.length(),t;
r=0;
for(int i=0;i<len;i++)
{ t=s[i];
r=(256*r+t)%34943;
}
r=(r*radix)%34943;
if(r==0)
{
cout<<"00 00"<<endl;
continue;
}
r=34943-r;
int b=r%256,a=r/256,c;
c=a/16;
if(c>9)cout<<(char)('A'+c-10);
else cout<<c;
c=a%16;
if(c>9)cout<<(char)('A'+c-10);
else cout<<c;
cout<<" ";
c=b/16;
if(c>9)cout<<(char)('A'+c-10);
else cout<<c;
c=b%16;
if(c>9)cout<<(char)('A'+c-10);
else cout<<c;
cout<<endl;
}
return 0;
}

55
HDOJ/1627_autoAC.cpp Normal file
View File

@ -0,0 +1,55 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=10001;
int n,m,ans[maxn];
void DFS(int cur)
{
for(int i=0;i<m;i++)
{
ans[cur]=i;
bool is=false;
for(int j=1;2*j<=cur+1;j++)
{
bool flag=0;
for(int k=0;k<j;k++)
if(ans[cur-j-k]!=ans[cur-k])
{
flag=1;
break;
}
if(!flag)
{
is=true;
break;
}
}
if(is)
continue;
if(--n==0)
{
for(int i=0;i<=cur;i++)
{
if(i&&i%64==0)
printf("\n");
else if(i&&i%4==0)
printf(" ");
printf("%c",'A'+ans[i]);
}
printf("\n%d\n",cur+1);
return;
}
DFS(cur+1);
if(!n)
return;
}
}
int main()
{
while(scanf("%d%d",&n,&m)&&(n||m))
{
DFS(0);
}
return 0;
}

18
HDOJ/1628_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <iostream>
#include <vector>
using namespace std;
int main(void) {
for (int n, k; cin >> n >> k && n * k != 0; ) {
vector<int> Circle;
for (int i = 0; i < n; Circle.push_back(++i));
for (int m = (k - 1) % Circle.size(), t; Circle.size() != 1; ) {
t = (m - 1 + k) % (Circle.size() - 1);
t = (t + (t >= m)) % Circle.size();
Circle[m] = Circle[t];
Circle.erase(Circle.begin() + t);
m = (m - (t < m) + k) % Circle.size();
}
cout << (n - Circle.front() + 1) % n + 1 << endl;
}
return 0;
}

135
HDOJ/1629_autoAC.cpp Normal file
View File

@ -0,0 +1,135 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <string>
using namespace std;
struct Card
{
char num;
char flower;
int num_flag;
};
Card p[5],q[5];
char str[]= {'A','2','3','4','5','6','7','8','9','T','J','Q','K'};
string s_name[9]= {"straight-flush","four-of-a-kind","full-house","flush",
"straight","three-of-a-kind","two-pairs","one-pair","highest-card"
};
int flag[15],count,value[15], solve_flag[10];
void solve(Card *r,int x)
{
if(solve_flag[0]) return;
int flag_flush(0);
memset(value,0,sizeof(value));
memset(flag,0,sizeof(flag));
for(int j=0; j<13; j++)
if(r[0].num==str[j])
{
flag[j]=1;
if(!j) flag[13]=1;
value[j]++;
break;
}
for(int i=1; i<5; i++)
{
if(r[i-1].flower!=r[i].flower) flag_flush=1;
for(int j=0; j<13; j++)
if(r[i].num==str[j])
{
flag[j]=1;
if(!j) flag[13]=1;
value[j]++;
break;
}
}
if(!flag_flush) solve_flag[3]=1;
int y(0),v(0);
if(flag[0])
{
for(int i=0; i<5; i++)
if(flag[i]) y++;
if(y==5&&!flag_flush)
{
solve_flag[0]=1;
return;
}
if(y==5&&flag_flush) solve_flag[4]=1;
}
y=0;
for(int i=1; i<14; i++)
{
if(flag[i]) y++;
if(y==5&&!flag_flush)
{
solve_flag[0]=1;
return;
}
if(y==5&&flag_flush)
{
solve_flag[4]=1;
break;
}
if(y>=1&&!flag[i]) break;
}
y=v=0;
for(int i=0; i<14; i++)
if(value[i]>=4) solve_flag[1]=1;
else if(value[i]==3) v++;
else if(value[i]==2) y++;
if(v&&y==1) solve_flag[2]=1;
if(v&&!y) solve_flag[5]=1;
if(!v&&y==2) solve_flag[6]=1;
if(!v&&y==1) solve_flag[7]=1;
if(x<count)
for(int i=0; i<5; i++)
if(!r[i].num_flag)
{
Card m[5];
for(int j=0; j<5; j++) m[j]=r[j];
m[i]=q[x];
m[i].num_flag=1;
solve(m,x+1);
}
}
int main()
{
char c,s[100];
count=0;
while(scanf("%c",&c)!=EOF)
{
if(c=='\n'||count>9)
{
if(c!='\n') gets(s);
count=count-5;
printf("Hand: ");
for(int i=0; i<5; i++) printf("%c%c ",p[i].num,p[i].flower);
printf("Deck: ");
for(int i=0; i<count; i++) printf("%c%c ",q[i].num,q[i].flower);
if(c!='\n') printf("%s ",s);
printf("Best hand: ");
memset(solve_flag,0,sizeof(solve_flag));
solve_flag[8]=1;
solve(p,0);
for(int i=0; i<9; i++)
if(solve_flag[i])
{
cout<<s_name[i]<<endl;
break;
}
count=0;
}
else if(c!=' ')
{
Card k;
k.num=c;
scanf("%c",&c);
k.flower=c;
k.num_flag=0;
if(count<=4) p[count]=k;
else q[count-5]=k;
count++;
}
}
return 0;
}

72
HDOJ/1631_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include <iostream>
#include <string>
#include <vector>
using namespace std;
enum SYMBOL{A, MOD, LA, BA, DA, PREDA, NAM, SE, PC, P, PN, PS, ST, VP, PV, UN};
bool aVowel[] = {1,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0};
static SYMBOL aConvTbl[14][4] = {
{PREDA, UN, PREDA, PREDA}, {PREDA, UN, UN, PS}, {NAM, UN, UN, PN},
{LA, UN, PS, PN}, {MOD, UN, PS, VP}, {A, PS, PS, PS}, {PS, UN, UN, P},
{DA, UN, P, PC}, {BA, PN, P, PC}, {VP, PN, UN, PV}, {PV, UN, PN, ST},
{PV, UN, UN, ST}, {PC, UN, UN, SE}, {ST, UN, UN, SE},
};
SYMBOL Token2Status(const string &str) {
int nNum = str.length(), cLast = str[nNum - 1];
if (!islower(cLast) || !aVowel[cLast - 'a']) {
return NAM;
}
switch (nNum) {
case 1: return A;
case 5:
nNum = aVowel[str[4] - 'a'];
nNum |= ((aVowel[str[0] - 'a'] << 4) | (aVowel[str[1] - 'a'] << 3));
nNum |= ((aVowel[str[2] - 'a'] << 2) | (aVowel[str[3] - 'a'] << 1));
return (nNum == 5 || nNum == 9) ? PREDA : UN;
case 2:
switch (str[0]) {
case 'g': return MOD;
case 'b': return BA;
case 'd': return DA;
case 'l': return LA;
}
}
return UN;
}
int main(void) {
vector<SYMBOL> Set;
for (string str; cin >> str && str != "#";) {
int nDot = str.find('.');
if (nDot == str.npos) {
Set.push_back(Token2Status(str));
continue;
}
str.erase(str.length() - 1);
if (!str.empty()) {
Set.push_back(Token2Status(str));
}
for (int i = 0; i < 14; ++i) {
SYMBOL *pTbl = aConvTbl[i];
for (vector<SYMBOL>::iterator j = Set.begin(); j != Set.end();) {
vector<SYMBOL>::iterator iBeg = Set.begin(), iEnd = Set.end();
if (*j != pTbl[0]) {
++j;
continue;
}
if (pTbl[1] != UN && (j == iBeg || *(j - 1) != pTbl[1])) {
++j;
continue;
}
if (pTbl[2] != UN && (j == iEnd - 1 || *(j + 1) != pTbl[2])) {
++j;
continue;
}
j = pTbl[1] != UN ? Set.erase(j - 1) : j;
j = pTbl[2] != UN ? Set.erase(j + 1) - 1 : j;
*j = pTbl[3];
}
}
cout << (Set.size() == 1 && Set[0] == SE ? "Good" : "Bad!") << endl;
Set.clear();
}
return 0;
}

80
HDOJ/1632_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
using namespace std;
#define N 155
#define eps 1e-8
struct TPoint
{
double x,y;
}s;
struct TPolygon
{
int n;
TPoint p[N];
}ply1,ply2,ans;
double maxdist;
int dblcmp(double a) {return a<-eps?-1:a>eps?1:0;}
double MAX(double a,double b) {return a>b?a:b;}
double cross(TPoint a,TPoint b,TPoint c)
{
return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double plyarea(TPolygon res)
{
int i;
double area=0;
res.p[res.n]=res.p[0];
for(i=0;i<res.n;i++)
area+=cross(s,res.p[i],res.p[i+1]);
return area;
}
TPolygon cut(TPolygon res,TPoint s,TPoint e)
{
int d1,d2,i,t;
double s1,s2;
TPoint a;
TPolygon b;
b.n=0;
res.p[res.n]=res.p[0];
for(i=0,t=0;i<res.n;i++)
{
d1=dblcmp(s1=cross(res.p[i],s,e));
d2=dblcmp(s2=cross(res.p[i+1],s,e));
if(d1>=0) b.p[t++]=res.p[i];
if(d1*d2<0)
{
a.x=(s2*res.p[i].x-s1*res.p[i+1].x)/(s2-s1);
a.y=(s2*res.p[i].y-s1*res.p[i+1].y)/(s2-s1);
b.p[t++]=a;
}
}
b.n=t;
return b;
}
int main()
{
while(scanf("%d",&ply1.n),ply1.n)
{
int i;
double sumarea,area;
for(i=0;i<ply1.n;i++) scanf("%lf%lf",&ply1.p[i].x,&ply1.p[i].y);
s.x=0; s.y=0;
if((dblcmp(sumarea=plyarea(ply1)))<0) reverse(ply1.p,ply1.p+ply1.n);
ply1.p[ply1.n]=ply1.p[0];
sumarea=fabs(sumarea);
scanf("%d",&ply2.n);
for(i=0;i<ply2.n;i++) scanf("%lf%lf",&ply2.p[i].x,&ply2.p[i].y);
if((dblcmp(area=plyarea(ply2)))<0) reverse(ply2.p,ply2.p+ply2.n);
sumarea+=fabs(area);
ply2.p[ply2.n]=ply2.p[0];
ans=ply1;
for(i=0;i<ply2.n;i++)
ans=cut(ans,ply2.p[i],ply2.p[i+1]);
area=fabs(plyarea(ans));
printf("%8.2lf",sumarea/2-area);
}
puts("");
return 0;
}

80
HDOJ/1633_autoAC.cpp Normal file
View File

@ -0,0 +1,80 @@
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;
struct Point
{
double x;
double y;
};
Point p[3];
//姹㈢Н缁瀵瑰
double cross(Point O,Point A,Point B)
{
return fabs((A.x - O.x) * (B.y - O.y) - (B.x - O.x) * (A.y - O.y))/2;
}
bool judge()
{
for(int i=0; i<3; i++)
{
if(p[i].x!=0 || p[i].y!=0)
{
return true;
}
}
return false;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
while(scanf(" %lf %lf %lf %lf %lf %lf",&p[0].x,&p[0].y,&p[1].x,&p[1].y,&p[2].x,&p[2].y) && judge())
{
double maxX = 0,minX = 150;
double maxY = 0,minY = 150;
for(int i=0; i<3; i++)
{
if(p[i].x>maxX)
maxX = p[i].x;
if(p[i].y>maxY)
maxY = p[i].y;
if(p[i].x<minX)
minX = p[i].x;
if(p[i].y<minY)
minY = p[i].y;
}
//姹涓瑙褰㈢㈢Н
double area = cross(p[0],p[1],p[2]);
int num = 0;
for(int i=ceil(minX); i<=floor(maxX); i++)
{
if(i <= 0 || i>=100)
{
continue;
}
for(int j=ceil(minY); j<=floor(maxY); j++)
{
if(j <= 0 || j>=100)
{
continue;
}
double s = 0;
Point temp;
temp.x = i;
temp.y = j;
s += cross(temp,p[0],p[1]);
s += cross(temp,p[1],p[2]);
s += cross(temp,p[2],p[0]);
if(fabs(area-s)<=1e-8)
{
num++;
}
}
}
printf("%4d\n",num);
}
}

70
HDOJ/1635_autoAC.cpp Normal file
View File

@ -0,0 +1,70 @@
#include <algorithm>
#include <functional>
#include <iostream>
#include <string>
using namespace std;
static int aMDays[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
static int aMDaysL[] = {0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335};
bool LeapYear(int Year, bool bNew) {
if (bNew) {
return ((Year % 4 == 0 && Year % 100 != 0) || Year % 400 == 0);
}
return (Year % 4 == 0);
}
int Date2Days(int Year, int Month, int Date, bool bNew) {
--Year;
int Days = Year * 365 + Year / 4 + (bNew ? (Year / 400 - Year / 100) : 0);
Days += ((Month > 2) ? LeapYear(Year + 1, bNew) : 0);
return (Days + aMDays[Month - 1] + Date - 1 - (!bNew * 2));
}
int main(void) {
const static string aDays[] = {"Sunday", "Monday", "Tuesday",
"Wednesday", "Thursday", "Friday", "Saturday"};
const static string aMonths[] = {"January", "February", "March",
"April", "May", "June", "July", "August", "September",
"October", "November", "December"};
const int nDays400Y = 400 * 365 + 100 - 3;
const int nDays100Y = 100 * 365 + 25 - 1;
const int nDays4Y = 4 * 365 + 1, nDays1Y = 365;
for (string str; cin >> str && str != "#";) {
int Day, Date, Month, Year;
Day = find(&aDays[0], &aDays[7], str) - &aDays[0];
cin >> Date >> str >> Year;
Month = find(&aMonths[0], &aMonths[12], str) - &aMonths[0] + 1;
int nDays = Date2Days(Year, Month, Date, false);
bool bOld2New = true;
if (Day == (nDays + 1) % 7) {
Year = nDays / nDays400Y * 400;
nDays %= nDays400Y;
if (nDays == nDays100Y * 4) {
Year += 300;
nDays -= nDays100Y * 3;
}
else {
Year += nDays / nDays100Y * 100;
nDays %= nDays100Y;
}
}
else {
nDays = Date2Days(Year, Month, Date, true) + 2;
Year = bOld2New = 0;
}
Year += nDays / nDays4Y * 4;
nDays %= nDays4Y;
if (nDays == nDays1Y * 4) {
Year += 3;
nDays -= nDays1Y * 3;
}
else {
Year += nDays / nDays1Y;
nDays %= nDays1Y;
}
int *pMDays = (LeapYear(++Year, bOld2New) ? aMDaysL : aMDays);
for (Month = 0; Month < 12 && pMDays[Month] <= nDays; ++Month);
Date = nDays - pMDays[Month - 1] + 1;
Day = (Date2Days(Year, Month, Date, bOld2New) + 1) % 7;
cout << aDays[Day] << ' ' << Date << (bOld2New ? " " : "* ");
cout << aMonths[Month - 1] << ' ' << Year << endl;
}
return 0;
}

67
HDOJ/1638_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <stack>
#include <map>
#include <list>
#include <queue>
using namespace std;
map<char,int> mma;
int main(){
mma['A'] = 14;
mma['J'] = 11;
mma['Q'] = 12;
mma['K'] = 13;
for(char i = '2' ; i <='9'; i++) mma[i] = i-'0';
char nuse,card;
while(cin >> nuse && nuse != '#'){
vector<int> tmp[2];
queue<int> player[2];
list<int> stk;
cin >> card;
tmp[1].push_back(mma[card]);
for(int i = 2; i <= 52; i++){
cin >> nuse >> card;
tmp[i%2].push_back(mma[card]);
}
for(int i = 0; i < 2; i++){
while(tmp[i].size()){
player[i].push(tmp[i][tmp[i].size()-1]);
tmp[i].pop_back();
}
}
int turn = 1,winner;
while(1){
int times;
bool flag = 0;
if(stk.size()==0) times = 0;
else if(stk.back()>=11) times = stk.back()-10;
else times = 0;
int cnt;
if(times == 0) cnt = 1;
else cnt = times;
for(int i = 0; i < cnt; i++){
if(player[turn].size()==0){
winner = !turn;
flag = 1;
break;
}else{
stk.push_back(player[turn].front());
player[turn].pop();
if(stk.back()>=11) break;
}
}
if(flag) break;
if(times && stk.back()<=10){
while(stk.size()){
player[!turn].push(stk.front());
stk.pop_front();
}
}
turn = !turn;
}
printf("%d%3d\n",winner+1,player[winner].size());
}
return 0;
}

53
HDOJ/1641_autoAC.cpp Normal file
View File

@ -0,0 +1,53 @@
#include<stdio.h>
#include<string.h>
int H,K,ans[15],res;
int judge(int target,int cur,int num,int tot,int *A,int end)
{
int i,j;
if(tot==target)
return 1;
else if(cur==end||num==H)
return 0;
if(judge(target,cur+1,num,tot,A,end))
return 1;
if(judge(target,cur,num+1,tot+A[cur],A,end))
return 1;
return 0;
}
void dfs(int cur,int *A,int end)
{
int i,j;
if(judge(cur,0,0,0,A,end))
dfs(cur+1,A,end);
if(end<K)
{
A[end]=cur;
end++;
dfs(cur+1,A,end);
end--;
A[end]=-1;
}
if(cur-1>res)
{
res=cur-1;
memcpy(ans,A,sizeof(ans));
}
}
int main()
{
int i,j,k,A[15];
while(1)
{
scanf("%d%d",&H,&K);
if(H==0)
break;
memset(A,-1,sizeof(A));
res=0;
dfs(1,A,0);
for(i=0;i<K;i++)
printf("%3d",ans[i]);
printf(" ->");
printf("%3d\n",res);
}
return 0;
}

38
HDOJ/1650_autoAC.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <algorithm>
#include <iostream>
#include <vector>
typedef std::vector<int>::iterator VECINT_ITER;
int Josephus(int n, int k)
{
static std::vector<int> vecJosephus(1, 0);
if (n <= (int)vecJosephus.size())
return vecJosephus[n - 1];
int j = (Josephus(n - 1, k) + k) % n;
vecJosephus.push_back(j);
return j;
}
int main(void)
{
for (int i = 0; i < 100000; Josephus(++i, 15));
for (int nSizeMin, nSizeMax; std::cin >> nSizeMin >> nSizeMax;)
{
if (nSizeMin > nSizeMax)
std::swap(nSizeMin, nSizeMax);
if (nSizeMin == 0 && nSizeMax == 0)
break;
std::vector<int> vecCover(nSizeMin / 2, 0);
for (int nSize = nSizeMin, nJump = 15; nSize <= nSizeMax; ++nSize) {
int j = Josephus(nSize, nJump);
if (j > nSize / 2)
j = nSize - j;
if (j < vecCover.size())
vecCover[j] = 1;
}
VECINT_ITER ir = std::find(vecCover.begin() + 1, vecCover.end(), 0);
if (ir == vecCover.end())
std::cout << "Better estimate needed" << std::endl;
else
std::cout << ir - vecCover.begin() << std::endl;
}
return 0;
}

65
HDOJ/1659_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <string.h>
struct node
{int x[50],y[50],z,num;
} a[1000][1000];
int visit[1000][1000];
int dfs(int X,int Y)
{int i;
if (a[X][Y].z==0) return a[X][Y].num;
else
{
for (i=1;i<=a[X][Y].z;i++)
a[X][Y].num=a[X][Y].num+dfs(a[X][Y].x[i],a[X][Y].y[i]);
a[X][Y].z=0;
return a[X][Y].num;
}
}
int main()
{int f,t,l,i,j,k,n,m,num,top,tail,column,row,pos;
char s[2000];
scanf("%d",&t);
while (t--)
{scanf("%d %d",&n,&m);
memset(visit,0,sizeof(visit));
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
{a[i][j].z=0;a[i][j].num=0;}
for (i=1;i<=m;i++)
{getchar();
for (j=1;j<=n;j++)
{scanf("%s",&s);
l=strlen(s);
a[i][j].z=0;
if (s[0]!='=')
{num=0; f=0; if (s[0]=='-') f=1;
for (k=f;k<l;k++)
num=num*10+s[k]-'0';
a[i][j].num=num;
if (f==1) a[i][j].num=-a[i][j].num;
visit[i][j]=1;
}
else
{column=0; row=0; pos=1;
a[i][j].num=0;
while (pos<l)
{while ((s[pos]>='A')&&(s[pos]<='Z')) {column=column*26+s[pos]-'A'+1; ++pos;}
while ((pos<l)&&(s[pos]>='0')&&(s[pos]<='9')) {row=row*10+s[pos]-'0'; ++pos;}
if ((visit[row][column]==1)||((i==row)&&(j==column))) a[i][j].num=a[i][j].num+a[row][column].num;
else {++a[i][j].z;a[i][j].x[a[i][j].z]=row; a[i][j].y[a[i][j].z]=column;}
row=0; column=0; ++pos;
}
}
}
}
for (i=1;i<=m;i++)
for (j=1;j<=n;j++)
if (a[i][j].z>0) dfs(i,j);
for (i=1;i<=m;i++)
{for (j=1;j<n;j++)
printf("%d ",a[i][j].num);
printf("%d\n",a[i][n].num);
}
}
return 0;
}

58
HDOJ/1661_autoAC.cpp Normal file
View File

@ -0,0 +1,58 @@
#include<iostream>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<math.h>
#include<stdlib.h>
using namespace std;
const int maxn = 1003;
struct Point
{
int x,y;
double rad;
bool operator<(const Point &rhs) const {
return rad < rhs.rad;
}
}P[maxn];
int col[maxn];
Point tmp[maxn];
bool cmp(const Point& a,const Point& b) //anticlockwise sort
{
return a.x*b.y >= b.x*a.y;
}
int solve(int n)
{
if(n<=3) return n;
int ans = -1;
for(int pivot = 0; pivot < n; pivot++){
int k = 0;
for(int i = 0; i < n; i++) if(i!=pivot){
tmp[k].x = P[i].x - P[pivot].x;
tmp[k].y = P[i].y - P[pivot].y;
if(col[i]) { tmp[k].x = - tmp[k].x; tmp[k].y = -tmp[k].y; }
tmp[k].rad = atan2(tmp[k].y, tmp[k].x);
k++;
}
sort(tmp,tmp+k);
int L = 0, R = 0, sum = 1;
while(L<k){
if(L == R) { R = (R+1)%k; sum++; }
while(R != L && cmp(tmp[L],tmp[R])) {
R = (R+1)%k; sum++;
}
ans = max(ans,sum);
sum--; L++;
}
}
return ans;
}
int main()
{
int n;
while(~scanf("%d",&n)&&n){
for(int i = 0; i < n; i++)
scanf("%d%d%d",&P[i].x,&P[i].y,col+i);
printf("%d\n",solve(n));
}
return 0;
}

164
HDOJ/1662_autoAC.cpp Normal file
View File

@ -0,0 +1,164 @@
#include<cstring>
#include<cassert>
#include<cstdio>
#include<cctype>
#include<ctime>
#include<cmath>
#include<functional>
#include<algorithm>
#include<iostream>
#include<vector>
#include<string>
#include<bitset>
#include<queue>
#include<deque>
#include<stack>
#include<list>
#include<map>
#include<set>
using namespace std;
typedef long long int64;
typedef unsigned long long uint64;
typedef pair <int, int> PII;
typedef pair <int64, int64> PLL;
typedef pair <char, char> PCC;
typedef pair <double, double> PDD;
#define MP make_pair
#define PB push_back
#define SZ(a) ((int)(a).size())
#define X first
#define Y second
#define L(x) ((x)<<1)
#define R(x) ((x)<<1 | 1)
#define max3(x, y, z) (max(max((x), (y)), (z)))
#define min3(x, y, z) (min(min((x), (y)), (z)))
#define BIT(x, i) (((x) >> (i)) & 1)
#define FOR(it, c) for( TYPE((c).begin()) it = (c).begin(); it != (c).end(); it++)
const double PI = 2.0*acos(0.0);
const double EPS = 1e-6;
#define GREATER(x, y) ((x) > (y) + EPS)
#define GREATER_EQUAL(x, y) ((x) > (y) - EPS)
#define LESS(x, y) ((x) < (y) - EPS)
#define LESS_EQUAL(x, y) ((x) < (y) + EPS)
#define EQUAL(x, y) (abs((x) - (y)) < EPS)
#define MAX_N 35
#define MAX_M 1005
#define MAXX 0x3f
#define UPPER 2147483647LL
#define INT_MAX 2147483647
#define INF (1<<29)
#define BINF (1LL<<60)
#define NONE -1
#define NIL 0
struct data{
char alpha;
char dir;
data(){}
data(char alpha, char dir) :alpha(alpha), dir(dir){}
} tile[MAX_N][MAX_N];
const int OFFR[] = { 1, 0, -1, 0 };
const int OFFC[] = { 0, 1, 0, -1 };
int n, m;
bool try_to_remove(int sx, int sy, int line_cnt){
bool visit[2][MAX_N][MAX_N];
memset(visit, false, sizeof(visit));
visit[0][sx][sy] = true;
int p = 0;
for (int ln = 1; ln <= line_cnt; ln++){
vector <PII> V;
memset(visit[p ^ 1], false, sizeof(visit[p ^ 1]));
for (int r = 0; r <= n + 1; r++) for (int c = 0; c <= m + 1; c++) if (visit[p][r][c]){
visit[p ^ 1][r][c] = true;
for (int k = 0; k < 4; k++){
int xx = r + OFFR[k];
int yy = c + OFFC[k];
while (xx >= 0 && xx <= n + 1 && yy >= 0 && yy <= m + 1){
if (tile[xx][yy].alpha != '.'){
if (xx == sx && yy == sy)
break;
if (tile[xx][yy].alpha == tile[sx][sy].alpha)
V.PB(MP(xx, yy));
break;
}
else
visit[p ^ 1][xx][yy] = true;
xx += OFFR[k];
yy += OFFC[k];
}
}
}
sort(V.begin(), V.end());
if (SZ(V) > 0){
tile[sx][sy].alpha = tile[V[0].X][V[0].Y].alpha = '.';
return true;
}
p ^= 1;
}
return false;
}
bool refresh(){
data tmp[MAX_N][MAX_N];
for (int r = 1; r <= n; r++) for (int c = 1; c <= m; c++)
tmp[r][c] = tile[r][c];
bool rsh = false;
for (int r = 1; r <= n; r++) for (int c = 1; c <= m; c++) if (tile[r][c].alpha != '.'){
int ver = 4;
switch (tile[r][c].dir){
case 'D': ver = 0; break;
case 'R': ver = 1; break;
case 'U': ver = 2; break;
case 'L': ver = 3; break;
}
if (ver <= 3){
int pr = r, pc = c;
int nr = pr + OFFR[ver];
int nc = pc + OFFC[ver];
if (nr >= 1 && nr <= n && nc >= 1 && nc <= m && tmp[nr][nc].alpha == '.'){
swap(tmp[pr][pc], tmp[nr][nc]);
rsh = true;
}
}
}
for (int r = 1; r <= n; r++) for (int c = 1; c <= m; c++)
tile[r][c] = tmp[r][c];
return rsh;
}
void solve(){
while (true){
bool move = false;
for (int ln = 1; ln <= 3 && !move; ln++){
for (int r = 1; r <= n && !move; r++) for (int c = 1; c <= m && !move; c++){
if (tile[r][c].alpha != '.' && try_to_remove(r, c, ln))
move = true;
}
}
if (move)
while (refresh());
else
break;
}
}
int main(){
int T = 0;
while (~scanf("%d %d", &n, &m) && n + m){
char tmp[5];
for (int r = 0; r <= n+1; r++){
for (int c = 0; c <= m+1; c++){
if (r == 0 || r == n + 1 || c == 0 || c == m + 1)
tile[r][c] = data('.', 'S');
else{
scanf("%s", tmp);
tile[r][c] = data(tmp[0], tmp[1]);
}
}
}
solve();
printf("Case %d\n", ++T);
for (int r = 1; r <= n; r++){
for (int c = 1; c <= m; c++)
putchar(tile[r][c].alpha);
puts("");
}
}
return 0;
}

40
HDOJ/1663_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int a,b;
long long A[10],B[10];
void dp(int s,long long *p,int t)
{
if(s<=0) return;
int x=s/10,y=s%10;
for(int i=1;i<=y;i++) p[i]+=t;
while(x)
{
p[x%10]+=(y+1)*t;
x/=10;
}
for(int i=0;i<=9;i++) p[i]+=t*(s/10);
t*=10;
dp(s/10-1,p,t);
}
int main()
{
while(scanf("%d%d",&a,&b)&&b!=0)
{
memset(A,0,sizeof(A));
memset(B,0,sizeof(B));
if(a<b)
swap(a,b);
b--;
if(a>b)
{
dp(a,A,1);
dp(b,B,1);
}
for(int i=0;i<9;i++)
printf("%I64d ",A[i]-B[i]);
printf("%I64d\n",A[9]-B[9]);
}
return 0;
}

110
HDOJ/1664_autoAC.cpp Normal file
View File

@ -0,0 +1,110 @@
#include<stdio.h>
const int N = 65536;
const int M = N+N;
int x[2][N]={0},q[M]={0},p[M]={0};
int bfs(int a,int b,int n,int t)
{
int fr=0,r=1,i=0;
char f[N]={0};
q[0]=0;
if (a==0)
{
fr=2;
q[2]=b;
r=3;
f[0]=1;
p[2]=0;
}
while(fr<r)
{
if (f[q[fr]]==1)
{
fr++;
continue;
}
f[q[fr]]=1;
q[r]=(q[fr]*10+a)%n;
p[r]=fr;
if (q[r]==0)
break;
r++;
q[r]=(q[fr]*10+b)%n;
p[r]=fr++;
if (q[r]==0)
break;
r++;
}
if (fr<r)
{
for (i=0;r!=0;i++)
{
if (r%2==1)
x[t][i]=a;
else x[t][i]=b;
r=p[r];
}
return i-1;
}
return -1;
}
int main()
{
int n,d[10],c,i,j,k,m,t;
scanf("%d",&n);
while (n!=0)
{
d[0]=n;
c=0;
for (i=1;i<10;i++)
{
j=i;
d[i]=1;
while (d[i]<n && j%n!=0)
{
j=j%n*10+i;
d[i]++;
}
if (d[i]<d[c])
c=i;
}
if (c!=0)
{
for (j=d[c];j>0;j--)
printf("%d",c);
}
else{
m=n;
t=0;
for (i=0;i<10;i++)
{
for (j=i+1;j<10;j++)
{
c=bfs(i,j,n,t);
if (c!=-1 && c<m)
{
t=1-t;
m=c;
}
else if (c==m)
{
for (k=m;k>=0;k--)
{
if (x[t][k]<x[1-t][k])
{
t=1-t;
break;
}
else if (x[t][k]>x[1-t][k])
break;
}
}
}
}
for (i=m;i>=0;i--)
printf("%d",x[1-t][i]);
}
printf("\n");
scanf("%d",&n);
}
return 0;
}

68
HDOJ/1667_autoAC.cpp Normal file
View File

@ -0,0 +1,68 @@
#include <cstdio>
#define size 24
using namespace std;
const int opt[][size] =
{
{ 2, 1, 6, 3, 4, 5, 11, 7, 8, 9, 10, 15, 12, 13, 14, 20, 16, 17, 18, 19, 22, 21, 0, 23 },
{ 0, 3, 2, 8, 4, 5, 6, 7, 12, 9, 10, 11, 17, 13, 14, 15, 16, 21, 18, 19, 20, 23, 22, 1 },
{ 0, 1, 2, 3, 10, 4, 5, 6, 7, 8, 9, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 19, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23 },
{ 0, 23, 2, 1, 4, 5, 6, 7, 3, 9, 10, 11, 8, 13, 14, 15, 16, 12, 18, 19, 20, 17, 22, 21 },
{ 22, 1, 0, 3, 4, 5, 2, 7, 8, 9, 10, 6, 12, 13, 14, 11, 16, 17, 18, 19, 15, 21, 20, 23 },
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 15, 16, 17, 18, 19, 13, 20, 21, 22, 23 },
{ 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 4, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 },
{ 6, 7, 8, 11, 12, 15, 16, 17 },
};
const char optlist[] = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H' };
int sharp[size];
char path[size];
bool judge(int node[]){
for (int i = 0; i < 8; i++)
if (node[opt[8][0]] != node[opt[8][i]])
return false;
return true;
}
int get_h(int node[]){
int cnt[4] = { 0 };
for (int i = 0; i < 8; i++)
cnt[node[opt[8][i]]]++;
int max = 0;
for (int i = 1; i <= 3; i++)
max < cnt[i] ? max = cnt[i] : 0;
return 8 - max;
}
bool ida(int curr[], int expr, int depth, char path[]){
if (expr == depth) {
if (judge(curr)) {
path[expr] = '\0';
printf("%s\n%d\n", path, curr[6]);
return true;
}
else
return false;
}
if (get_h(curr) + expr > depth) return false;
int next[size];
for (int i = 0; i < 8; i++){
for (int j = 0; j < size; j++) next[j] = curr[opt[i][j]];
path[expr] = optlist[i];
if (ida(next, expr + 1, depth, path)) return true;
}
return false;
}
int main(){
while (scanf("%d", sharp), sharp[0]){
for (int i = 1; i < size; i++){
scanf("%d", sharp + i);
}
if (judge(sharp)){
printf("No moves needed\n%d\n", sharp[6]);
continue;
}
for (int i = get_h(sharp);; i++){
if (ida(sharp, 0, i, path))
break;
}
}
return 0;
}

86
HDOJ/1668_autoAC.cpp Normal file
View File

@ -0,0 +1,86 @@
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
#define N 13
#define M 8192
int f[M][N][N];
ll g[M][N][N];
int a[N][N];
int v[N];
int t, n, m;
void dp()
{
if (n == 1)
{
cout << v[0] << " " << 1 << endl;
return ;
}
memset(f, 0, sizeof(f));
memset(g, 0, sizeof(g));
for (int i = 0; i < n; ++i)
{
f[1 << i][i][i] = v[i];
g[1 << i][i][i] = 1;
}
m = (1 << n) - 1;
for (int s = 0; s < m; ++s)
for (int i = 0; 1 << i <= s; ++i)
if ((s >> i) & 1)
for (int j = 0; 1 << j <= s; ++j)
if (((s >> j) & 1) && f[s][i][j])
for (int k = 0; k < n; ++k)
if (!((s >> k) & 1) && a[j][k])
{
int temp = f[s][i][j] + v[k] + v[j] * v[k];
if (i != j && a[i][k])
temp += v[i] * v[j] * v[k];
int ss = s + (1 << k);
if (temp > f[ss][j][k])
{
f[ss][j][k] = temp;
g[ss][j][k] = g[s][i][j];
}
else if (temp == f[ss][j][k])
{
g[ss][j][k] += g[s][i][j];
}
}
int ans = 0;
ll ans2 = 0;
for (int i = 0; i < n; ++i)
for (int j = 0; j < n; ++j)
if (f[m][i][j] > ans)
{
ans = f[m][i][j];
ans2 = g[m][i][j];
}
else if (f[m][i][j] == ans)
{
ans2 += g[m][i][j];
}
cout << ans << " " << ans2 / 2 << endl;
}
int main()
{
ios::sync_with_stdio(0);
scanf("%d", &t);
while (t--)
{
memset(a, 0, sizeof(a));
scanf("%d%d", &n, &m);
for (int i = 0; i < n; ++i)
scanf("%d", &v[i]);
while (m--)
{
int x, y;
scanf("%d%d", &x, &y);
--x, --y;
a[x][y] = a[y][x] = 1;
}
dp();
}
return 0;
}

89
HDOJ/1669_autoAC.cpp Normal file
View File

@ -0,0 +1,89 @@
#include <cstdio>
#include <cstring>
#define N 1005
#define M 505
#define CLR(a,v) memset(a,v,sizeof(a))
struct Edge
{
int v,next;
}edge[N*M];
int ehead[N];
int ecnt;
inline void AddEdge(int u,int v)
{
edge[ecnt]={v,ehead[u]};
ehead[u]=ecnt++;
}
int match[M][N];
int mcnt[N];
bool vis[M];
int limit;
bool Find(int u)
{
for(int i=ehead[u];~i;i=edge[i].next)
{
int v=edge[i].v;
if(vis[v])continue;
vis[v]=true;
if(mcnt[v]<limit)
{
match[v][mcnt[v]++]=u;
return true;
}
for(int j=0;j<mcnt[v];j++)if(Find(match[v][j]))
{
match[v][j]=u;
return true;
}
}
return false;
}
bool Match(int n)
{
CLR(mcnt,0);
for(int i=0;i<n;i++)
{
CLR(vis,false);
if(!Find(i))return false;
}
return true;
}
bool lof;
bool Int(int &a)
{
if(lof)return lof=false;
char c;
for(c=getchar();c<'0'||c>'9';c=getchar())if(c=='\n')return false;
a=c-'0';
for(c=getchar();c>='0'&&c<='9';c=getchar())a=a*10+c-'0';
lof=c=='\n';
return true;
}
char s[20];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m),n)
{
CLR(ehead,-1);ecnt=0;
for(int i=0;i<n;i++)
{
scanf("%s",s);
int a;
lof=false;
while(Int(a))AddEdge(i,a);
}
int l=1,r=n;
while(l<r)
{
int m=(l+r)/2;
limit=m;
if(Match(n))
r=m;
else
l=m+1;
}
printf("%d\n",l);
}
return 0;
}

37
HDOJ/1671_autoAC.cpp Normal file
View File

@ -0,0 +1,37 @@
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdio>
using namespace std;
int main()
{
vector<string> v;
char ch[10];
string s;
int n;
cin>>n;
int i,j;
while(cin>>n)
{
v.clear();
for(i=0;i<n;i++)
{
scanf("%s",&ch);
s=ch;
v.push_back(s);
}
sort(v.begin(),v.end());
for(i=0;i<n-1;i++)
{
if(v[i+1].find(v[i])==0)
{
cout<<"NO"<<endl;
goto RL;
}
}
cout<<"YES"<<endl;
RL:continue;
}
return 0;
}

51
HDOJ/1672_autoAC.cpp Normal file
View File

@ -0,0 +1,51 @@
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int maxn=1e4+10;
int n,m,map[maxn][3],color[maxn];
bool vis[maxn];
bool DFS(int x)
{
for(int i=0;i<2;i++)
{
int ita=map[x][i];
if(!vis[ita])
{
vis[ita]=1;
if(color[ita]==-1||DFS(color[ita]))
{
color[ita]=x;
return true;
}
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(color,-1,sizeof(color));
scanf("%d%d",&m,&n);
for(int i=0;i<m;i++)
scanf("%d%d",&map[i][0],&map[i][1]);
int flag=0;
for(int i=0;i<m;i++)
{
memset(vis,0,sizeof(vis));
if(!DFS(i))
{
flag=1;
break;
}
}
if(flag)
printf("rehash necessary\n");
else
printf("successful hashing\n");
}
return 0;
}

18
HDOJ/1673_autoAC.cpp Normal file
View File

@ -0,0 +1,18 @@
#include <stdio.h>
int main()
{
int T,N,min,max,a;
scanf("%d",&T);
while(T--){
scanf("%d",&N);
min = 0x3f3f3f3f;
max = -0x3f3f3f3f;
while(N--){
scanf("%d",&a);
if(a>max) max = a;
if(a<min) min = a;
}
printf("%d\n",(max-min)*2);
}
return 0;
}

98
HDOJ/1676_autoAC.cpp Normal file
View File

@ -0,0 +1,98 @@
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#define M 1007
using namespace std;
int n,m,cap,s,e,num;
int val[M],dp[M][M],head[M];
struct node
{
int u,cost,res;
bool operator < (const node a)const
{
return cost>a.cost;
}
};
struct E
{
int v,to,dist;
} edg[M*20];
vector<int>G[M];
void addedge(int u,int v,int dist)
{
edg[num].v=v;
edg[num].dist=dist;
edg[num].to=head[u];
head[u]=num++;
}
void init()
{
num=0;
memset(head,-1,sizeof(head));
memset(val,0,sizeof(val));
for(int i=0; i<n; i++)
G[i].clear();
}
void bfs()
{
for(int i=0; i<n; i++)
for(int j=0; j<=cap; j++)
dp[i][j]=0;
priority_queue<node> q;
q.push((node){s,0,0});
while(!q.empty())
{
node x=q.top();
q.pop();
int u=x.u,cost=x.cost,res=x.res;
if(u==e)
{
printf("%d\n",cost);
return ;
}
if(res<cap&&(dp[u][res+1]==0||dp[u][res+1]>cost+val[u]))
{
dp[u][res+1]=cost+val[u];
q.push((node){u,dp[u][res+1],res+1});
}
for(int i=head[u]; i!=-1; i=edg[i].to)
{
int v=edg[i].v,dist=edg[i].dist;
if(res<dist)continue;
if(dp[v][res-dist]==0||dp[v][res-dist]>cost)
{
dp[v][res-dist]=cost;
q.push((node){v,cost,res-dist});
}
}
}
printf("impossible\n");
return ;
}
int main()
{
while( scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=0; i<n; i++)
scanf("%d",&val[i]);
int a,b,c;
for(int i=1; i<=m; i++)
{
scanf("%d%d%d",&a,&b,&c);
addedge(a,b,c);
addedge(b,a,c);
}
int q;
scanf("%d",&q);
while(q--)
{
scanf("%d%d%d",&cap,&s,&e);
bfs();
}
}
return 0;
}

46
HDOJ/1677_autoAC.cpp Normal file
View File

@ -0,0 +1,46 @@
#include <iostream>
#include <cstring>
using namespace std;
struct node
{
int x, y;
};
int cmp (const void *a, const void *b)
{
node *c = (node *)a;
node *d = (node *)b;
if (c -> x != d -> x)
return c -> x - d -> x;
else
return d -> y - c -> y;
}
int main ()
{
node stu[21000];
int t, n, i, j, k, a[21000];
cin >> t;
while (t--)
{
memset (a, 0, sizeof(a));
cin >> n;
for (i=0; i<n; i++)
cin >> stu[i].x >> stu[i].y;
qsort (stu, n, sizeof (stu[0]), cmp);
a[0] = stu[0].y, k = 1;
for (i=1; i<n; i++)
{
for (j=0; j<k; j++)
{
if (a[j] < stu[i].y)
{
a[j] = stu[i].y;
break;
}
}
if (j == k)
a[k++] = stu[i].y;
}
cout << k << endl;
}
return 0;
}

23
HDOJ/1678_autoAC.cpp Normal file
View File

@ -0,0 +1,23 @@
#include<stdio.h>
#include<algorithm>
using namespace std;
bool cmp(int x,int y)
{ return x>y;
}
int main()
{ int t,n,i,sum;
int a[20005];
scanf("%d",&t);
while(t--)
{ scanf("%d",&n);sum=0;
for(i=1;i<=n;i++)
{ scanf("%d",&a[i]);
}
sort(a+1,a+n+1,cmp);
for(i=3;i<=n;i+=3)
{ sum+=a[i];
}
printf("%d\n",sum);
}
return 0;
}

49
HDOJ/1679_autoAC.cpp Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <string.h>
#include <math.h>
#define inf 1e9;
int h,in[210];
double e[210][210];
double dp[210][210];
double min(double a,double b){
return a>b?b:a;
}
int main(){
int t,T,h,c,i,j,k;
scanf("%d",&T);
for(t=1;t<=T;t++){
scanf("%d %d",&h,&c);
for(i=1;i<=h;i++)
scanf("%d",&in[i]);
for(i=1;i<=h;i++)
for(j=i;j<=h;j++){
if(j==i){
e[i][j]=0;
continue;
}
double tem =(double)(in[j]-in[i])/(j-i);
double sum=0;
for(int k=i+1;k<j;k++)
sum+=fabs(tem*(k-i)+in[i]-in[k]);
e[i][j]=sum;
}
for(i=1;i<=h;i++)
for(j=1;j<=c;j++){
dp[i][j] = inf;
}
for(i=2;i<=h;i++){
for(j=2;j<=c && j<=i;j++){
if(j==2){
dp[i][j]=e[1][i];
continue;
}
for(k=2;k<i;k++){
if(k>=j-1){
dp[i][j]=min(dp[i][j],dp[k][j-1]+e[k][i]);
}
}
}
}
printf("%.4f\n",dp[h][c]/h);
}
}

105
HDOJ/1680_autoAC.cpp Normal file
View File

@ -0,0 +1,105 @@
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define N 100
char map[10][10];
int wx,wy,hx,hy;
int visit[10][10][100];
int dir[8][2]= {0,1,1,1,1,0,1,-1,0,-1,-1,-1,-1,0,-1,1};
struct node
{
int x;
int y;
int step;
} a[N];
bool judge(node a)
{
if(a.x==hx+a.step&&(a.y==hy+1||a.y==hy-1))return false;
if(map[a.x][a.y]=='F'||map[a.x][a.y]=='D')return false;
return a.x>=1&&a.y>=1&&a.x<=8&&a.y<=8&&!visit[a.x][a.y][a.step];
}
int bfs()
{
memset(visit,0,sizeof(visit));
int front=0;
int rear=1;
node cur;
a[0].x=wx;
a[0].y=wy;
a[0].step=0;
visit[wx][wy][0]=1;
int flag=1;
int mm=0;
while(front<rear)
{
node cur;
cur.x=a[front].x;
cur.y=a[front].y;
cur.step=a[front].step;
if(map[hx+cur.step][hy]=='F')
{
return 1;
}
if(hx+cur.step>8)
{
return 0;
}
for(int i=0; i<8; i++)
{
int tx=cur.x+dir[i][0];
int ty=cur.y+dir[i][1];
int tstep=cur.step+1;
node b;
b.step=tstep;
b.x=tx;
b.y=ty;
if((tx>=1 && tx<=8) && (ty>=1 && ty<=8) && map[tx][ty]!='F' && map[tx][ty]!='D' && visit[tx][ty][tstep]==0)
{
if(!((tx==hx+tstep && ty==hy-1) || (tx==hx+tstep && ty==hy+1)))
{
visit[tx][ty][tstep]=1;
if(tx==hx+cur.step&&ty==hy||tx==hx+tstep&&ty==hy)
{
return 1;
}
node tmp;
tmp.x=tx;
tmp.y=ty;
tmp.step=tstep;
a[rear++]=tmp;
}
}
}
front++;
}
return 0;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
for(int i=1; i<=8; i++)
{
scanf("%s",map[i]+1);
}
scanf("%d%d",&wx,&wy);
int xx=wx;
wx=9-wy;
wy=xx;
scanf("%d%d",&hx,&hy);
xx=hx;
hx=9-hy;
hy=xx;
int ans=bfs();
if(ans==1)
{
printf("White\n");
}
else
printf("Black\n");
}
return 0;
}

35
HDOJ/1681_autoAC.cpp Normal file
View File

@ -0,0 +1,35 @@
#include<cstdio>
#include<cstring>
bool dp[1010010];
int main()
{
int i,a[5],t,count,mem;
scanf("%d",&t);
while(t--)
{
scanf("%d%d%d%d",a,a+1,a+2,a+3);
memset(dp,false,sizeof(dp));
dp[a[0]]=dp[a[1]]=dp[a[2]]=dp[a[3]]=1;
for(count=0,i=1;i<1000001;i++)
{
if(dp[i])
{
dp[i+a[0]]=dp[i+a[1]]=dp[i+a[2]]=dp[i+a[3]]=1;
}
else count++,mem=i;
}
for(;i<1010000;i++)
{
if(!dp[i])mem=-1;
else
{
if(i+a[0]<1010000)dp[i+a[0]]=1;
if(i+a[1]<1010000)dp[i+a[1]]=1;
if(i+a[2]<1010000)dp[i+a[2]]=1;
if(i+a[3]<1010000)dp[i+a[3]]=1;
}
}
printf("%d\n%d\n",count,mem);
}
return 0;
}

36
HDOJ/1683_autoAC.cpp Normal file
View File

@ -0,0 +1,36 @@
#include <iostream>
#include <cstring>
using namespace std;
char a[101],b[101],c[101];
int main()
{
int t,i;
cin>>t;
while(t--)
{
cin>>a>>b>>c;
int len1=strlen(a);
int len2=strlen(b);
int len3=strlen(c);
int j=0;
for(i=0;i<=len2;i++)
{
if(b[i]=='*'||c[i]=='*')
{
j++;
}
else if (a[j]==b[i]||a[j]==c[i])
{
j++;
}
if(j==len1)
{
cout<<"win"<<endl;
break;
}
}
if(i>len2)
cout<<"lose"<<endl;
}
return 0;
}

67
HDOJ/1684_autoAC.cpp Normal file
View File

@ -0,0 +1,67 @@
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
int dp[101][101];
int n,m,sal;
struct pro
{
int p[101],pun,rew;
}pr[101];
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d%d",&m,&n,&sal);
for(int i=1;i<=m;i++)
{
pr[i].p[0]=0;
for(int j=1;j<=n;j++)
scanf("%d",&pr[i].p[j]);
scanf("%d %d",&pr[i].rew,&pr[i].pun);
}
memset(dp,0,sizeof(dp));
dp[0][0]=0;
for(int i=1;i<=m;i++)
dp[i][0]=dp[i-1][0]-pr[i].pun*100;
for(int i=1;i<=n;i++)
dp[1][i]=(pr[1].rew-sal*i)*pr[1].p[i]-(100-pr[1].p[i])*pr[1].pun;
for(int i=2;i<=m;i++)
{
for(int j=1;j<=n;j++)
{
dp[i][j]=-INT_MAX;
for(int k=0;k<=j;k++)
{
int temp=dp[i-1][j-k]+(pr[i].rew-sal*k)*pr[i].p[k]-(100-pr[i].p[k])*pr[i].pun;
if(temp>dp[i][j])
dp[i][j]=temp;
}
}
}
int max1=-INT_MAX;
for(int i=0;i<=n;i++)
{
if(dp[m][i]>max1)
max1=dp[m][i];
}
printf("%d\n",max1);
int flag=1;
for(int i=0;i<=n;i++)
{
if(max1==dp[m][i])
{
if(flag)
{
printf("%d",i);
flag=0;
}
else printf(" %d",i);
}
}
printf("\n");
}
return 0;
}

65
HDOJ/1685_autoAC.cpp Normal file
View File

@ -0,0 +1,65 @@
#include <stdio.h>
#include <math.h>
int n,num[15],lim;
bool flag;
int h()
{
int i,r=0;
if(num[0]!=1) r++;
if(num[n-1]!=n) r++;
for(i=0;i<n-1;i++) if(num[i]+1!=num[i+1]) r++;
return r;
}
void dfs(int cnt)
{
if(flag) return;
int i;
for(i=0;i<n;i++) if(num[i]!=i+1) break;
if(i==n)
{
flag=1;
return;
}
if(cnt>=lim) return;
if(cnt+ceil(h()/3.0)>lim) return;
int len,pos,j,p,temp[15];
for(i=0;i<n;i++) temp[i]=num[i];
for(i=0;i<n;i++)
{
for(len=1;len<=n-i;len++)
{
for(pos=0;pos<=n-len;pos++)
{
if(pos==i) continue;
for(j=0;j<len;j++) num[pos+j]=temp[i+j];
for(p=0,j=0;j<n;)
{
if(pos==j) j+=len;
if(j>=n) break;
if(p==i) p+=len;
num[j++]=temp[p++];
}
dfs(cnt+1);
}
}
}
for(i=0;i<n;i++) num[i]=temp[i];
}
int main()
{
int T,i;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
for(i=0;i<n;i++) scanf("%d",&num[i]);
flag=0;
for(lim=0;lim<5;lim++)
{
dfs(0);
if(flag) break;
}
if(flag) printf("%d\n",lim);
else printf("5 or more\n");
}
}

64
HDOJ/1686_autoAC.cpp Normal file
View File

@ -0,0 +1,64 @@
#include <stdio.h>
#include <string.h>
#define MAX 10000
int next[MAX + 1];
char pattern[MAX + 1];
int init_next (char *pattern, int *next);
int KMP (char text, char *pattern, int len);
int main()
{
int T;
int len;
char text = '\0';
scanf ("%d", &T);
getchar ();
while (T --)
{
len = init_next (pattern, next);
printf ("%d\n", KMP (text, pattern, len));
}
return 0;
}
int init_next (char *pattern, int *next)
{
int q, i = 0;
q = next[0] = -1;
pattern[i ++] = getchar ();
while ((pattern[i] = getchar ()) != '\n')
{
while (q >= 0 && pattern[q + 1] != pattern[i])
{
q = next[q];
}
if (pattern[q + 1] == pattern[i])
{
q ++;
}
next[i] = q;
i ++;
}
return i;
}
int KMP (char text, char *pattern, int len)
{
int q, i = 0;
int count = 0;
q = -1;
while ((text = getchar ()) != '\n')
{
while (q >= 0 && pattern[q + 1] != text)
{
q = next[q];
}
if (pattern[q + 1] == text)
{
q ++;
}
if (q == len - 1)
{
q = next[q];
count ++;
}
}
return count;
}

41
HDOJ/1687_autoAC.cpp Normal file
View File

@ -0,0 +1,41 @@
#include <stdio.h>
#include <algorithm>
using std::sort;
using std::swap;
double X, Y;
struct Node{
double left, right;
} arr[102];
double getX(double a, double b){
return X - Y * (X - a) / (Y - b);
}
bool cmp(Node a, Node b){
return a.left < b.left;
}
int main(){
double x1, y1, x2, y2, flag;
int t, n, ans;
scanf("%d", &t);
while(t--){
scanf("%d", &n);
scanf("%lf%lf", &X, &Y);
for(int i = 0; i < n; ++i){
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
arr[i].left = getX(x1, y1);
arr[i].right = getX(x2, y2);
if(arr[i].left > arr[i].right) swap(arr[i].left, arr[i].right);
}
sort(arr, arr + n, cmp);
ans = 1;
flag = arr[0].right;
for(int i = 1; i < n; ++i){
if(flag < arr[i].left){
++ans;
flag = arr[i].right;
}else if(flag < arr[i].right) flag = arr[i].right;
}
if(!n) printf("1\n");
else printf("%d\n", ans + 1);
}
return 0;
}

92
HDOJ/1688_autoAC.cpp Normal file
View File

@ -0,0 +1,92 @@
#include<iostream>
#include<algorithm>
#include<vector>
#include<string.h>
using namespace std;
const int N = 1001;
struct node
{
int v,w;
node(){}
node(int v,int w):v(v),w(w){}
};
vector<node> g[N];
int n,dist[N][2],cnt[N][2];
bool vis[N][2];
void Dijkstra(int s)
{
for(int i=1;i<=n;i++)
{
dist[i][0]=INT_MAX;
dist[i][1]=INT_MAX;
}
memset(vis,false,sizeof(vis));
dist[s][0]=0;
cnt[s][0]=1;
for(int i=0;i<2*n;i++)
{
int temp=INT_MAX,u=-1,k;
for(int j=1;j<=n;j++)
if(!vis[j][0] && dist[j][0]<temp)
{
k=0;
temp=dist[j][0];
u=j;
}
else if(!vis[j][1] && dist[j][1]<temp)
{
k=1;
temp=dist[j][1];
u=j;
}
if(u==-1)
break;
vis[u][k]=true;
vector<node>::iterator it=g[u].begin();
for(;it!=g[u].end();it++)
{
int newdis=dist[u][k]+it->w;
int v=it->v;
if(newdis<dist[v][0])
{
dist[v][1]=dist[v][0];
cnt[v][1]=cnt[v][0];
dist[v][0]=newdis;
cnt[v][0]=cnt[u][k];
}
else if(newdis==dist[v][0])
cnt[v][0]+=cnt[u][k];
else if(newdis<dist[v][1])
{
dist[v][1]=newdis;
cnt[v][1]=cnt[u][k];
}
else if(newdis==dist[v][1])
cnt[v][1]+=cnt[u][k];
}
}
}
int main()
{
int T,m,a,b,c;
int s,t;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
g[i].clear();
for(int i=0;i<m;i++)
{
scanf("%d %d %d",&a,&b,&c);
g[a].push_back(node(b,c));
}
scanf("%d %d",&s,&t);
Dijkstra(s);
int ans=cnt[t][0];
if(dist[t][1]==dist[t][0]+1)
ans+=cnt[t][1];
printf("%d\n",ans);
}
return 0;
}

72
HDOJ/1689_autoAC.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<iostream>
#include<vector>
#include<queue>
#include<cstring>
#define maxn 10000+1
using namespace std;
vector<int>mapp[maxn];
int n,m;
int visit[maxn][2];
struct stu
{
int pre;
int now;
int deep;
};
int bfs(int p)
{
queue<stu>root;
stu x,y;
x.pre=-1;
x.now=p;
x.deep=0;
root.push(x);
while(root.size())
{
x=root.front();
root.pop();
if(x.now==p&&x.deep>=3&&x.deep%2) return x.deep;
else if(x.now==p&&x.deep!=0) continue;
for(int i=0;i<mapp[x.now].size();i++)
{
if(mapp[x.now][i]!=x.pre&&!visit[mapp[x.now][i]][(x.deep+1)%2])
{
y.pre=x.now;
y.now=mapp[x.now][i];
y.deep=x.deep+1;
visit[y.now][y.deep%2]=1;
root.push(y);
}
}
}
return 1<<30;
}
int main()
{
cin.sync_with_stdio(false);
int t;
cin>>t;
int casee=1;
while(t--)
{
cin>>n>>m;
for(int i=1;i<=n;i++) mapp[i].clear();
for(int i=0;i<m;i++)
{
int x,y;
cin>>x>>y;
mapp[x].push_back(y);
mapp[y].push_back(x);
}
int ans=1<<30;
for(int i=1;i<=n;i++)
{
memset(visit,0,sizeof(visit));
ans=min(ans,bfs(i));
}
cout<<"Case "<<casee++<<": ";
if(ans!=1<<30) cout<<"JYY has to use "<<ans<<" balls."<<endl;
else cout<<"Poor JYY."<<endl;
}
return 0;
}

63
HDOJ/1690_autoAC.cpp Normal file
View File

@ -0,0 +1,63 @@
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
using namespace std;
const long long INF = 1e18;
const int maxn = 105;
long long map[maxn][maxn];
long long L1, L2, L3, L4;
long long C1, C2, C3, C4;
int n;
long long cost(long long dis)
{
if(dis > 0 && dis <= L1)return C1;
if(dis > L1 && dis <= L2)return C2;
if(dis > L2 && dis <= L3)return C3;
if(dis > L3 && dis <= L4)return C4;
return INF;
}
void floyd()
{
int i, j, k;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
{
if(map[i][j] > map[i][k] + map[k][j])
map[i][j] = map[i][k] + map[k][j];
}
}
int main()
{
int ncase, k=1;
cin >> ncase;
while(ncase--)
{
int m, a, b, i, j;
long long f[maxn]={0};
cin >> L1 >> L2 >> L3 >> L4;
cin >> C1 >> C2 >> C3 >> C4;
cin >> n >> m;
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
map[i][j] = INF;
for(i=1; i<=n; i++)
cin >> f[i];
for(i=1; i<n; i++)
for(j=i+1; j<=n; j++)
map[i][j] = map[j][i] = cost(abs(f[i]-f[j]));
floyd();
cout << "Case " << k++ << ":" <<endl;
for(i=0; i<m; i++)
{
cin >> a >> b;
if(map[a][b] != INF)
cout <<"The minimum cost between station "<< a <<" and station "<< b <<" is "<< map[a][b] <<"."<<endl;
else
cout <<"Station "<< a <<" and station "<< b <<" are not attainable."<<endl;
}
}
return 0;
}

259
HDOJ/1691_autoAC.cpp Normal file
View File

@ -0,0 +1,259 @@
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include <string.h>
#include <stdlib.h>
int map[20][20];
int Kinga,Kingb;
int Kingc,Kingd;
int abs(int a){return a>0?a:-a;}
int KingFaceToFace()
{
int i;
if(Kingb==20||Kingd==20)return 0;
if(Kingb==Kingd)
{
for(i=Kingc+1;i<Kinga;i++)
if(map[i][Kingb]!=0)
return 0;
return 1;
}
return 0;
}
int King(int a,int b,int c,int d)
{
if((abs(a-c)+abs(b-d))!=1)return 0;
if(map[a][b]==1&&(a<8||b<4||b>6||c<8||d<4||d>6))
return 0;
if(map[a][b]==8&&(a>3||b<4||b>6||c>3||d<4||d>6))
return 0;
if(map[a][b]==1){
Kinga=c;
Kingb=d;
}
if(map[a][b]==8){
Kingc=c;
Kingd=d;
}
map[c][d]=map[a][b];
map[a][b]=0;
if(KingFaceToFace())
return 0;
return 1;
}
int Mandarins(int a,int b,int c,int d)
{
if(abs(a-c)!=1||abs(b-d)!=1)return 0;
if(map[a][b]==2&&(c<8||c>10||d<4||d>6))
return 0;
if(map[a][b]==9&&(c>3||c<1||d<4||d>6))
return 0;
map[c][d]=map[a][b];
map[a][b]=0;
if(KingFaceToFace())
return 0;
return 1;
}
int Elephants(int a,int b,int c,int d)
{
if(abs(a-c)!=2||abs(b-d)!=2)return 0;
if(map[a][b]==3&&(!(((a==6||a==10)&&(b==3||b==7))||(a==8&&(b==1||b==5||b==9)))||!(((c==6||c==10)&&(d==3||d==7))||(c==8&&(d==1||d==5||d==9)))))return 0;
if(map[a][b]==10&&(!(((a==5||a==1)&&(b==3||b==7))||(a==3&&(b==1||b==5||b==9)))||!(((c==5||c==1)&&(d==3||d==7))||(c==3&&(d==1||d==5||d==9)))))return 0;
int x,y;
x=(a+c)/2;
y=(b+d)/2;
if(map[x][y])
return 0;
map[c][d]=map[a][b];
map[a][b]=0;
if(KingFaceToFace())
return 0;
return 1;
}
int Knights(int a,int b,int c,int d)
{
int luoma[8][2]={{2,1},{2,-1},{-2,1},{-2,-1},{1,2},{1,-2},{-1,2},{-1,-2}};
int majiao[8][2]={{1,0},{1,0},{-1,0},{-1,0},{0,1},{0,-1},{0,1},{0,-1}};
int i;
for(i=0;i<8;i++)
{
if(a+luoma[i][0]==c&&b+luoma[i][1]==d)
{
if(map[a+majiao[i][0]][b+majiao[i][1]])
return 0;
break;
}
}
if(i==8)return 0;
if(map[c][d]==1)Kinga=Kingb=20;
if(map[c][d]==8)Kingc=Kingd=20;
map[c][d]=map[a][b];
map[a][b]=0;
if(KingFaceToFace())
return 0;
return 1;
}
int Rooks(int a,int b,int c,int d)
{
int i;
if(a!=c&&b!=d)return 0;
if(a==c&&d>b)
for(i=b+1;i<d;i++)
if(map[a][i])
return 0;
if(a==c&&d<b)
for(i=b-1;i>d;i--)
if(map[a][i])
return 0;
if(b==d&&a>c)
for(i=a-1;i>c;i--)
if(map[i][b])
return 0;
if(b==d&&a<c)
for(i=a+1;i<c;i++)
if(map[i][b])
return 0;
if(map[c][d]==1)Kinga=Kingb=20;
if(map[c][d]==8)Kingc=Kingd=20;
map[c][d]=map[a][b];
map[a][b]=0;
if(KingFaceToFace())
return 0;
return 1;
}
int Cannons(int a,int b,int c,int d)
{
int i,cnt=0;
if(a!=c&&b!=d)return 0;
if(a==c&&d>b)
{
for(i=b+1;i<d;i++)
if(map[a][i]!=0)
cnt++;
if(cnt>1||(cnt==1&&map[c][d]==0)||(cnt==0&&map[c][d]))return 0;
}
if(a==c&&d<b)
{
for(i=b-1;i>d;i--)
if(map[a][i]!=0)
cnt++;
if(cnt>1||(cnt==1&&map[c][d]==0)||(cnt==0&&map[c][d]))return 0;
}
if(b==d&&a>c)
{
for(i=a-1;i>c;i--)
if(map[i][b]!=0)
cnt++;
if(cnt>1||(cnt==1&&map[c][d]==0)||(cnt==0&&map[c][d]))return 0;
}
if(b==d&&a<c)
{
for(i=a+1;i<c;i++)
if(map[i][b]!=0)
cnt++;
if(cnt>1||(cnt==1&&map[c][d]==0)||(cnt==0&&map[c][d]))return 0;
}
if(map[c][d]==1)Kinga=Kingb=20;
if(map[c][d]==8)Kingc=Kingd=20;
map[c][d]=map[a][b];
map[a][b]=0;
if(KingFaceToFace())
return 0;
return 1;
}
int Pawns(int a,int b,int c,int d)
{
if(abs(a-c)+abs(b-d)!=1)return 0;
if(map[a][b]==14&&a<=5)
if(c-a!=1||b!=d)
return 0;
if(map[a][b]==7&&a>=6)
if(c-a!=-1||b!=d)
return 0;
if(map[a][b]==14&&a>5)
if(c-a==-1)
return 0;
if(map[a][b]==7&&a<6)
if(c-a==1)
return 0;
if(map[c][d]==1)Kinga=Kingb=20;
if(map[c][d]==8)Kingc=Kingd=20;
map[c][d]=map[a][b];
map[a][b]=0;
if(KingFaceToFace())
return 0;
return 1;
}
int move(int a,int b,int c,int d,int k)
{
if(map[a][b]<=k*7||map[a][b]>k*7+7)
return 0;
if(a>10||b>9||c>10||d>9||a<1||b<1||c<1||d<1)return 0;
if(map[a][b]==0)return 0;
if((map[a][b]==1||map[a][b]==2||map[a][b]==3||map[a][b]==4||map[a][b]==5||map[a][b]==6||map[a][b]==7)&&(map[c][d]==1||map[c][d]==2||map[c][d]==3||map[c][d]==4||map[c][d]==5||map[c][d]==6||map[c][d]==7))
return 0;
if((map[a][b]==8||map[a][b]==9||map[a][b]==10||map[a][b]==11||map[a][b]==12||map[a][b]==13||map[a][b]==14)&&(map[c][d]==8||map[c][d]==9||map[c][d]==10||map[c][d]==11||map[c][d]==12||map[c][d]==13||map[c][d]==14))
return 0;
if(map[a][b]==1||map[a][b]==8)
return King(a,b,c,d);
if(map[a][b]==2||map[a][b]==9)
return Mandarins(a,b,c,d);
if(map[a][b]==3||map[a][b]==10)
return Elephants(a,b,c,d);
if(map[a][b]==4||map[a][b]==11)
return Knights(a,b,c,d);
if(map[a][b]==5||map[a][b]==12)
return Rooks(a,b,c,d);
if(map[a][b]==6||map[a][b]==13)
return Cannons(a,b,c,d);
if(map[a][b]==7||map[a][b]==14)
return Pawns(a,b,c,d);
}
int main()
{
int t;
int n,k;
int a,b,c,d;
int i,j;
int nCase=1;
int step;
scanf("%d",&t);
while(t--)
{
for(i=1;i<=10;i++)
{
for(j=1;j<=9;j++)
{
scanf("%d",&map[i][j]);
if(map[i][j]==1){
Kinga=i;
Kingb=j;
}
if(map[i][j]==8){
Kingc=i;
Kingd=j;
}
}
}
scanf("%d%d",&n,&k);
step=0;
for(i=1;i<=n;i++)
{
scanf("%d%d%d%d",&a,&b,&c,&d);
if(!step)
{
if(!move(a,b,c,d,k))
step=i;
if(i!=n&&(Kingb==20||Kingd==20))
step=i+1;
}
k=!k;
}
printf("Case %d: ",nCase++);
if(!step)
printf("Legal move\n");
else
printf("Illegal move on step %d\n",step);
}
return 0;
}

34
HDOJ/1692_autoAC.cpp Normal file
View File

@ -0,0 +1,34 @@
#include<iostream>
using namespace std;
int w[100005],L[100005],p[100005];
int main()
{
int T,i,N,j,max,k,sum,carry;
cin>>T;
for(i=1;i<=T;i++)
{
cin>>N;
for(j=1;j<=N;j++)
cin>>w[j]>>L[j]>>p[j];
max=p[N];
for(j=1;j<N;j++)
{
sum=0;
carry=0;
for(k=j;k<=N;k++)
{
carry=carry+w[k];
if(carry<=L[k])
sum=sum+p[k];
if(sum>max)
break;
}
if(sum<max)
{
max=sum;
}
}
cout<<"Case "<<i<<": Need to use "<<max<<" mana points."<<endl;
}
return 0;
}

66
HDOJ/1693_autoAC.cpp Normal file
View File

@ -0,0 +1,66 @@
#include <cstdio>
#include <cstring>
int tree[13][13];
long long int dp[13][13][1<<13];
int main()
{
int t;
scanf("%d",&t);
for(int p=1;p<=t;p++)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
for(int j=1;j<=m;j++)
{
scanf("%d",&tree[i][j]);
}
}
memset(dp,0,sizeof(dp));
dp[0][m][0]=1;
for(int i=1;i<=n;i++)
{
for(int k=0;k<(1<<(m+1));k++)
{
dp[i][0][k<<1]=dp[i-1][m][k];
}
for(int j=1;j<=m;j++)
{
int x=1<<j;
int y=1<<(j-1);
for(int k=0;k<(1<<(m+1));k++)
{
if(tree[i][j]!=0)
{
if((k&x)!=0 && (k&y)!=0)
{
dp[i][j][k]=dp[i][j-1][k^x^y];
}
else if((k&x)==0 && (k&y)==0)
{
dp[i][j][k]=dp[i][j-1][k^x^y];
}
else
{
dp[i][j][k]=dp[i][j-1][k]+dp[i][j-1][k^x^y];
}
}
else
{
if((k&x)==0 && (k&y)==0)
{
dp[i][j][k]=dp[i][j-1][k];
}
else
{
dp[i][j][k]=0;
}
}
}
}
}
printf("Case %d: There are %I64d ways to eat the trees.\n",p,dp[n][m][0]);
}
return 0;
}

40
HDOJ/1695_autoAC.cpp Normal file
View File

@ -0,0 +1,40 @@
#include <stdio.h>
#define G1 100005
int mu[G1];
void getMu(){
for(int i=1;i<G1;i++)
mu[i]=1;
for(int i=2;i<G1;i++){
if(mu[i]==0)continue;
mu[i]=-mu[i];
for(int j=i*2;j<G1;j+=i)
mu[j]+=mu[i];
}
}
int main(){
getMu();
int t1,t2,k,kase=1,temp,_;
long long ans1,ans2;
scanf("%d",&_);
while(_--){
scanf("%*d%d%*d%d%d",&t1,&t2,&k);
if(k==0){
printf("Case %d: 0\n",kase++);
continue;
}
t1=t1/k;
t2=t2/k;
if(t1>t2){
temp=t1;
t1=t2;
t2=temp;
}
ans1=ans2=0;
for(int i=1;i<=t1;i++)
ans1+=(long long)mu[i]*(t1/i)*(t2/i);
for(int i=1;i<=t1;i++)
ans2+=(long long)mu[i]*(t1/i)*(t1/i);
printf("Case %d: %I64d\n",kase++,ans1-ans2/2);
}
return 0;
}

28
HDOJ/1698_autoAC.cpp Normal file
View File

@ -0,0 +1,28 @@
#include<stdio.h>
const int n = 1000005;
int map[n][3];
int main()
{
int i,M,z,N,sum,v;
scanf("%d",&N);
for(i=1; i<=N; i++)
{
sum = 0;
int Q,j;
scanf("%d%d",&M,&Q);
for(j=1; j<=Q; j++)
scanf("%d%d%d",&map[j][0],&map[j][1],&map[j][2]);
for(j=1; j<=M; j++)
{
v = 1;
for(int k=Q; k>=0; k--)
if(map[k][0]<=j && j<=map[k][1])
{
v = map[k][2];
break;
}
sum += v;
}
printf("Case %d: The total value of the hook is %d.\n",i,sum);
}
}