HDU 1496 Train Problem I 火车问题1(桟,水)
2015-06-03 22:41
459 查看
题意:
给出两个串,串中的数字i 代表编号为i的火车进入车站的顺序,车站如桟一样,先进后出。第二个串是火车出站的顺序,问若按照第一个串那样进站,是否有可能如第二个串一样的出站顺序?火车顶多9辆,即1~9。
思路:
用桟模拟,每进入一辆火车就加到桟顶,判断第二个串中开头是否是此车,若是,两者都删掉,若不是,继续添加火车进桟,继续判断。直到判断匹配了,将桟顶出桟,串头删除,继续匹配桟顶和串头,直到不匹配了,继续进桟。这样一直反复,直到最后,桟中一定为空,第二个串也一定为空。以此判断是否符合条件。
AC代码
给出两个串,串中的数字i 代表编号为i的火车进入车站的顺序,车站如桟一样,先进后出。第二个串是火车出站的顺序,问若按照第一个串那样进站,是否有可能如第二个串一样的出站顺序?火车顶多9辆,即1~9。
思路:
用桟模拟,每进入一辆火车就加到桟顶,判断第二个串中开头是否是此车,若是,两者都删掉,若不是,继续添加火车进桟,继续判断。直到判断匹配了,将桟顶出桟,串头删除,继续匹配桟顶和串头,直到不匹配了,继续进桟。这样一直反复,直到最后,桟中一定为空,第二个串也一定为空。以此判断是否符合条件。
#include <bits/stdc++.h> using namespace std; int a, b, c, d; int has1[1000006]; int has2[1000006]; int qq[105][105][105][105]; int cal() { memset(has1,0,sizeof(has1)); memset(has2,0,sizeof(has2)); int ans=0; for(int x1=1; x1<=100; x1++) for(int x2=1; x2<=100; x2++) { int tmp=a*x1*x1+ b*x2*x2; if( tmp>=0 ) has1[tmp]++; else has2[-tmp]++; } for(int x3=1; x3<=100; x3++) { for(int x4=1; x4<=100; x4++) { int tmp=c*x3*x3+d*x4*x4; if(tmp>0) ans+=has2[tmp]; else ans+=has1[-tmp]; } } return (ans<<4); } int main() { //freopen("input.txt", "r", stdin); while(~scanf("%d%d%d%d",&a,&b,&c,&d)) { if(a>0 && b>0 && c>0 && d>0 || a<0 && b<0 && c<0 && d<0) { printf("0\n"); continue; } if(!qq[a+50][b+50][c+50][d+50]) { qq[a+50][b+50][c+50][d+50]=cal()+1; } printf("%d\n",qq[a+50][b+50][c+50][d+50]-1); } return 0; }
AC代码
相关文章推荐
- OC release之后 retainCount为何为1
- poj 3250 Bad Hair Day (单调栈)
- Light 1021 - Painful Bases (状压dp)
- Algorithms—219.Contains Duplicate II
- (转载)ADS启动问题:The unexpected failure of a COM operation suggests that MWComHelpers may be missing
- Contains Duplicate III
- poj 3250 Bad Hair Day 单调栈
- Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost.
- hdu - 1104 Remainder (bfs + 数论)
- Error: Failed to launch instance "win7": Please try again later [Error: No valid host was found. ].
- An animation block that calls the subviews updateConstraints method
- 帝国留言板管理员回复发送EMAIL通知客户
- 219. Contains Duplicate II
- ARC中Retain Cycle揭秘
- 217. Contains Duplicate
- AEAI DP_V3.4.0_20150601升级说明
- postfix+extmail+dovecot+ courier-authlib 实现公网邮件服务器
- 解决sublimeText3无法安装插件问题 -- There are no packages available for installation
- Leetcode Problem.217 —Contains Duplicate
- 使用NSSearchPathForDirectoriesInDomains等获取路径的方法时的注意事项