http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4378

写了这么长不容易纪念一下

  1. /********************* Template ************************/
  2. #include <set>
  3. #include <map>
  4. #include <list>
  5. #include <cmath>
  6. #include <ctime>
  7. #include <deque>
  8. #include <queue>
  9. #include <stack>
  10. #include <bitset>
  11. #include <cstdio>
  12. #include <string>
  13. #include <vector>
  14. #include <cassert>
  15. #include <cstdlib>
  16. #include <cstring>
  17. #include <sstream>
  18. #include <fstream>
  19. #include <numeric>
  20. #include <iomanip>
  21. #include <iostream>
  22. #include <algorithm>
  23. #include <functional>
  24. using namespace std;
  25. #define EPS 1e-8
  26. #define DINF 1e15
  27. #define MAXN 100050
  28. #define MOD 1000000007
  29. #define INF 0x7fffffff
  30. #define LINF 1LL<<60
  31. #define PI 3.14159265358979323846
  32. #define lson l,m,rt<<1
  33. #define rson m+1,r,rt<<1|1
  34. #define BUG cout<<" BUG! "<<endl;
  35. #define LINE cout<<" ------------------ "<<endl;
  36. #define FIN freopen("in.txt","r",stdin);
  37. #define FOUT freopen("out.txt","w",stdout);
  38. #define mem(a,b) memset(a,b,sizeof(a))
  39. #define FOR(i,a,b) for(int i = a ; i < b ; i++)
  40. #define read(a) scanf("%d",&a)
  41. #define read2(a,b) scanf("%d%d",&a,&b)
  42. #define read3(a,b,c) scanf("%d%d%d",&a,&b,&c)
  43. #define write(a) printf("%d\n",a)
  44. #define write2(a,b) printf("%d %d\n",a,b)
  45. #define write3(a,b,c) printf("%d %d %d\n",a,b,c)
  46. #pragma comment (linker,"/STACK:102400000,102400000")
  47. template<class T> inline T L(T a) {return (a << );}
  48. template<class T> inline T R(T a) {return (a << | );}
  49. template<class T> inline T lowbit(T a) {return (a & -a);}
  50. template<class T> inline T Mid(T a,T b) {return ((a + b) >> );}
  51. template<class T> inline T gcd(T a,T b) {return b ? gcd(b,a%b) : a;}
  52. template<class T> inline T lcm(T a,T b) {return a / gcd(a,b) * b;}
  53. template<class T> inline T ABS(T a) {return a > ? a : -a;}
  54. template<class T> inline T Min(T a,T b) {return a < b ? a : b;}
  55. template<class T> inline T Max(T a,T b) {return a > b ? a : b;}
  56. template<class T> inline T Min(T a,T b,T c) {return min(min(a,b),c);}
  57. template<class T> inline T Max(T a,T b,T c) {return max(max(a,b),c);}
  58. template<class T> inline T Min(T a,T b,T c,T d) {return min(min(a,b),min(c,d));}
  59. template<class T> inline T Max(T a,T b,T c,T d) {return max(max(a,b),max(c,d));}
  60. template<class T> inline T exGCD(T a, T b, T &x, T &y){
  61. if(!b) return x = ,y = ,a;
  62. T res = exGCD(b,a%b,x,y),tmp = x;
  63. x = y,y = tmp - (a / b) * y;
  64. return res;
  65. }
  66. template<class T> inline T reverse_bits(T x){
  67. x = (x >> & 0x55555555) | ((x << ) & 0xaaaaaaaa); x = ((x >> ) & 0x33333333) | ((x << ) & 0xcccccccc);
  68. x = (x >> & 0x0f0f0f0f) | ((x << ) & 0xf0f0f0f0); x = ((x >> ) & 0x00ff00ff) | ((x << ) & 0xff00ff00);
  69. x = (x >> & 0x0000ffff) | ((x <<) & 0xffff0000); return x;
  70. }
  71. typedef long long LL; typedef unsigned long long ULL;
  72. //typedef __int64 LL; typedef unsigned __int64 ULL;
  73. /********************* By F *********************/
  74. struct node{
  75. vector<int> v;
  76. int len;
  77. node(vector<int> x,int n){
  78. v = x;
  79. len = n;
  80. }
  81. void show(){
  82. for(int i = ; i < len ; i++) cout<<v[i]<<" ";
  83. cout<<endl;
  84. }
  85. bool operator == (const node &b){
  86. if(len != b.len) return false;
  87. for(int i = ; i < len ; i++)
  88. if(v[i] != b.v[i]) return false;
  89. return true;
  90. }
  91. };
  92. vector<int> t;
  93. node now = node(t,),res = node(t,);
  94. int n,m;
  95. bool flag;
  96. node fold(node x,int pos){
  97. int l = x.len;
  98. vector<int> vx;
  99. if(pos >= l/+) {
  100. for(int i = ; i < *pos-l ; i++){
  101. vx.push_back(x.v[i]);
  102. }
  103. for(int i = *pos-l ; i < pos ; i++){
  104. vx.push_back(x.v[i]+x.v[*pos--i]);
  105. }
  106. }else {
  107. for(int i = l- ; i >= *pos ; i--){
  108. vx.push_back(x.v[i]);
  109. }
  110. for(int i = ; i < *pos ; i++){
  111. vx.push_back(x.v[i]+x.v[*pos--i]);
  112. }
  113. }
  114. node y = (pos >= l/+) ? node(vx,pos) : node(vx,l-pos);
  115. return y;
  116. }
  117. void dfs(node x,int f){
  118. //x.show();
  119. if(flag) return;
  120. if(x == res){
  121. flag = ;
  122. return;
  123. }
  124. if(x.len == ) return;
  125. for(int i = ; i < x.len ;i++){
  126. node xx = fold(x,i);
  127. if(f == && xx.len == x.len)
  128. continue;
  129. else if(f == && xx.len == x.len)
  130. dfs(xx,);
  131. else dfs(xx,);
  132. }
  133. }
  134. int main(){
  135. FIN;
  136. //FOUT;
  137. while(scanf("%d",&n)!=EOF){
  138. flag = false;
  139. int p;
  140. vector<int> u,v;
  141. int sum1 = ,sum2 = ;
  142. for(int i = ; i < n ; i++){
  143. scanf("%d",&p);
  144. u.push_back(p);
  145. sum1 += p;
  146. }
  147. scanf("%d",&m);
  148. for(int i = ; i < m ; i++){
  149. scanf("%d",&p);
  150. v.push_back(p);
  151. sum2 += p;
  152. }
  153. if(sum1 != sum2){
  154. printf("N\n");
  155. continue;
  156. }
  157. now = node(u,n);
  158. res = node(v,m);
  159. dfs(now,);
  160. if(flag){
  161. printf("S\n");
  162. }else printf("N\n");
  163. }
  164. return ;
  165. }

UVA 12649 Folding Machine 搜索的更多相关文章

  1. UVA.129 Krypton Factor (搜索+暴力)

    UVA.129 Krypton Factor (搜索+暴力) 题意分析 搜索的策略是:优先找长串,若长串不合法,则回溯,继续找到合法串,直到找到所求合法串的编号,输出即可. 注意的地方就是合法串的判断 ...

  2. UVA - 1630 Folding(串折叠)(dp---记忆化搜索)

    题意:给出一个由大写字母组成的长度为n(1<=n<=100)的串,“折叠”成一个尽量短的串.折叠可以嵌套.多解时可输出任意解. 分析: 1.dp[l][r]为l~r区间可折叠成的最短串的长 ...

  3. uva 11624 Fire!(搜索)

    开始刷题啦= = 痛并快乐着,学到新东西的感觉其实比看那些无脑的小说.电视剧有意思多了 bfs裸体,关键是先把所有的着火点放入队列,分开一个一个做bfs会超时的 发现vis[][]是多余的,完全可以用 ...

  4. 模拟赛20181016 Uva 1040 状压+搜索 2005 ACM world final problem c

    题目的隐含条件将这道题指向了最小生成树: 利用类似prim的方法,枚举所有子图并判断是否包含询问点,如果包含那么可以更新答案: 边统计边更新,且由于更新一定是向更多的点状态下更新,所以一定可以统计到答 ...

  5. uva 707(记忆化搜索)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=21261 思路:此题需要记忆化搜索,dp[x][y][t]表示当前状 ...

  6. Power Calculus UVA - 1374 迭代加深搜索

    迭代加深搜索经典题目,好久不做迭代加深搜索题目,拿来复习了,我们直接对当前深度进行搜索,注意剪枝,还有数组要适当开大,因为2^maxd可能很大 题目:题目链接 AC代码: #include <i ...

  7. UVa 10118 记忆化搜索 Free Candies

    假设在当前状态我们第i堆糖果分别取了cnt[i]个,那么篮子里以及口袋里糖果的个数都是可以确定下来的. 所以就可以使用记忆化搜索. #include <cstdio> #include & ...

  8. UVA 1663 Purifying Machine (二分图匹配,最大流)

    题意: 给m个长度为n的模板串,模板串由0和1和*三种组成,且每串至多1个*,代表可0可1.模板串至多匹配2个串,即*号改成0和1,如果没有*号则只能匹配自己.问:模板串可以缩减为几个,同样可以匹配原 ...

  9. UVa 1630 Folding (区间DP)

    题意:折叠一个字符串,使得其成为一个尽量短的字符串  例如AAAAAA变成6(A) 而且这个折叠是可以嵌套的,例如 NEEEEERYESYESYESNEEEEERYESYESYES 会变成 2(N5( ...

随机推荐

  1. poj3134 Power Calculus IDA*

    好端端的一道搜索题目,,,硬生生的被我弄成了乱搞题,,,枚举当前的maxd,深搜结果,然而想到的剪枝方法都没有太好的效果,,,最后用一个贪心乱搞弄出来了,,, 贪心:每次必用上一次做出来的数字与其他数 ...

  2. 洛谷 P2027 bf

    P2027 bf 题目描述 bf是一种编程语言,全称为BrainFuck,因为题目名称不能太露骨,所以就简写成bf了. 这种语言的运行机制十分简单,只有一个大小为30000的有符号8位整数(范围[-1 ...

  3. Qt之QImageWriter

    简述 QImageWriter类为写入图像至文件或设备提供了一个独立的接口.QImageWriter支持格式特定的选项(如:质量和压缩率),可以在存储图像之前进行设置.如果不需要这些选项,可以使用QI ...

  4. zookeeper应用场景练习(分布式锁)

    在寻常的高并发的程序中.为了保证数据的一致性.因此都会用到锁.来对当前的线程进行锁定.在单机操作中.非常好做到,比方能够採用Synchronized.Lock或者其它的读写多来锁定当前的线程.可是在分 ...

  5. PermissionError: [Errno 13] in python

    出现该错误,首先尝试以管理员身份运行 cmd.exe 程序,然后关闭所有的与 python 相关的进程. 1. open 打开一个文件夹(目录),而不是文件 这一错误一般发生在使用 open函数对文件 ...

  6. PostgreSQL Replication之第六章 监控您的设置(2)

    6.2 检查pg_stat_replication 检查归档以及 archive_command主要用于即时恢复( PITR,Point-In-Time- Recovery).如果您想监控一个基于流的 ...

  7. webService接口发布失败问题

    今天在原有工程上新增加了个webService接口的服务类,但是总提示 axis2 出错 File "/axis2-web/listSingleService.jsp" not f ...

  8. WinDBG help

    WinDBG is a great, free tool. It is more powerful than Visual Studio's built-in debugger, but is har ...

  9. Python爬虫基础示例

    使用pip安装相关依赖: pip install requests pip install bs4 安装成功提示:Successfully installed *... 爬取中国天气网数据示例代码: ...

  10. OpenJDK源码研究笔记(八)-详细解析如何读取Java字节码文件(.class)

    在上一篇OpenJDK源码研究笔记(七)–Java字节码文件(.class)的结构中,我们大致了解了Java字节码文件的结构. 本篇详细地介绍了如何读取.class文件的大部分细节. 1.构造文件  ...