5510  Bazinga

题意:给出n个字符串,求满足条件的最大下标值或层数

条件:该字符串之前存在不是 它的子串 的字符串

求解si是不是sj的子串,可以用kmp算法之类的。

strstr是黑科技,比手写的kmp快。if(strstr(s[i], s[j]) == NULL),则Si不是Sj的子串。

还有一个重要的剪枝:对于一个串,如果当前找到的串是它的母串,则下一次这个串不用遍历。

  1. #include <set>
  2. #include <queue>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8. typedef long long LL;
  9. #define mem(x,y) memset(x, y, sizeof(x))
  10. #define lson l,m,rt << 1
  11. #define rson m+1,r,rt << 1 | 1
  12. ? a : gcd(b, a % b);}
  13. int lcm(int a,int b){return a / gcd(a, b) * b;}
  14.  
  15. int T, n;
  16. ][];
  17. ];
  18.  
  19. int main()
  20. {
  21. scanf("%d", &T);
  22. ; Case <= T; Case++)
  23. {
  24. mem(vis, );
  25. scanf("%d", &n);
  26. getchar();
  27. ; i <= n; i++)
  28. {
  29. scanf("%s", s[i]);
  30. }
  31. ;
  32. ; i <= n; i++)
  33. {
  34. ;
  35. ; j >= ; j--)
  36. {
  37. if(vis[j]) continue;
  38. if(strstr(s[i], s[j]) == NULL)//sj 不是 si的子串
  39. {
  40. ok = ;
  41. break;
  42. }
  43. else
  44. {
  45. vis[j] = ;//重要剪枝
  46. }
  47. }
  48. if(ok) ans = i;
  49. }
  50. printf("Case #%d: %d\n", Case, ans);
  51. }
  52. ;
  53. }

5512  Pagodas

题意:有n个庙经过长时间风吹雨打需要修补,只有两座(被标记为a,b)完好无损不需要修补,有两个和尚轮流去修补这n-2个庙,每个和尚每次只能修补一个庙标记为i,并要求i满足i=j+k或者i=j-k,每个庙只能被修建一次;其中j和k代表已经修建好的庙,Yuwgna先开始,问最后谁不能修建谁输;

更相减损术!(gcd里头:辗转相除法,更相减损术,自行百度)

更相减损术:

第一步:任意给定两个正整数;判断它们是否都是偶数。若是,则用2约简;若不是则执行第二步。
第二步:以较大的数减较小的数,接着把所得的差与较小的数比较,并以大数减小数。继续这个操作,直到所得的减数和差相等为止。
则第一步中约掉的若干个2与第二步中等数的乘积就是所求的最大公约数。
所以它在1-n里头,只要是它的最小公因数的倍数的数都是可以选择的点,所以答案是n / gcd(x, y)
主要是由i = j - k联想出来的。
  1. #include <set>
  2. #include <queue>
  3. #include <cstdio>
  4. #include <vector>
  5. #include <cstring>
  6. #include <algorithm>
  7. using namespace std;
  8. typedef long long LL;
  9. #define mem(x,y) memset(x, y, sizeof(x))
  10. #define lson l,m,rt << 1
  11. #define rson m+1,r,rt << 1 | 1
  12. ? a : gcd(b, a % b);}
  13. int T, n, x, y;
  14.  
  15. int main()
  16. {
  17. scanf("%d", &T);
  18. ; Case <= T; Case++)
  19. {
  20. scanf("%d%d%d", &n, &x, &y);
  21. int g = gcd(x, y);
  22. int cnt = n / g;
  23. printf("Case #%d: ", Case);
  24. printf( ? "Yuwgna" : "Iaka");
  25. }
  26. ;
  27. }

5521  Meeting

题意:给你一个n个点,m个集合的图,每个集合中的点都可以以di的距离相互的到达,问你两个人同时从1和n出发,会在那个点相遇。

每个集合内部里的所有的边都是互通的,如果一个个连就要n²了,可以采用增加虚拟结点的方式,来减少点。虚拟一个s,一个e,把他们连接起来。

  1. ; i <= m; i++)
  2. {
  3. int w, num;
  4. int s = n + i, e = n + i + m;
  5. scanf("%d%d", &w, &num);
  6. G[s].push_back(edge(e, w));
  7. while(num--)
  8. {
  9. int x;
  10. scanf("%d", &x);
  11. G[e].push_back(edge(x, ));
  12. G[x].push_back(edge(s, ));
  13. }
  14. }
  1. //题意:给你一个n个点,m个集合的图,每个集合中的点都可以以di的距离相互的到达,问你两个人同时从1和n出发,会在那个点相遇。
  2. #include <set>
  3. #include <queue>
  4. #include <cstdio>
  5. #include <vector>
  6. #include <cstring>
  7. #include <algorithm>
  8. using namespace std;
  9. typedef long long LL;
  10. #define mem(x,y) memset(x, y, sizeof(x))
  11. #define lson l,m,rt << 1
  12. #define rson m+1,r,rt << 1 | 1
  13. ? a : gcd(b, a % b);}
  14. int lcm(int a,int b){return a / gcd(a, b) * b;}
  15.  
  16. int T, n, m, Case;
  17. LL ans;
  18. ;
  19. const LL INF = 1e18;
  20.  
  21. ;
  22. LL d1[ssize], d2[ssize];
  23.  
  24. struct edge
  25. {
  26. int to;
  27. LL co;
  28. edge(int tt, LL cc):to(tt), co(cc){}
  29. bool operator < (const edge &other)const
  30. {
  31. return co > other.co;
  32. }
  33. };
  34. vector<edge>G[ssize];
  35. void init()
  36. {
  37. ; i <= n + * m; i++) G[i].clear();
  38. ans = INF;
  39. }
  40. void dijkstra(int s, LL dis[])
  41. {
  42. ; i <= n + * m; i++) dis[i] = INF;
  43. priority_queue<edge>que;
  44. dis[s] = ;
  45. que.push(edge(s, dis[s]));
  46. while(!que.empty())
  47. {
  48. edge p = que.top();que.pop();
  49. int v = p.to;
  50. if(dis[v] < p.co) continue;
  51. ; i < G[v].size(); i++)
  52. {
  53. edge e = G[v][i];
  54. if(dis[e.to] > dis[v] + e.co)
  55. {
  56. dis[e.to] = dis[v] + e.co;
  57. que.push(edge(e.to, dis[e.to]));
  58. }
  59. }
  60. }
  61. }
  62.  
  63. void solve()
  64. {
  65. dijkstra(, d1);
  66. dijkstra(n, d2);
  67. ans = INF;
  68. ; i <= n; i++)
  69. {
  70. ans = min(ans, max(d1[i], d2[i]));
  71. }
  72. printf("Case #%d: ", Case);
  73.  
  74. if(ans == INF)
  75. printf("Evil John\n");
  76. else
  77. {
  78. printf("%I64d\n", ans);
  79. ;
  80. ; i <= n; i++)
  81. {
  82. if(ans == max(d1[i], d2[i])) cnt++;
  83. }
  84. ; i <= n; i++)
  85. {
  86. if(ans == max(d1[i], d2[i])) cnt--, printf("%d%c", i, cnt ? ' ' : '\n' );
  87. }
  88. }
  89. }
  90.  
  91. int main()
  92. {
  93. scanf("%d", &T);
  94. ; Case <= T; Case++)
  95. {
  96. scanf("%d%d", &n, &m);
  97. init();
  98. ; i <= m; i++)
  99. {
  100. int w, num;
  101. int s = n + i, e = n + i + m;
  102. scanf("%d%d", &w, &num);
  103. G[s].push_back(edge(e, w));
  104. while(num--)
  105. {
  106. int x;
  107. scanf("%d", &x);
  108. G[e].push_back(edge(x, ));
  109. G[x].push_back(edge(s, ));
  110. }
  111. }
  112. solve();
  113. }
  114. ;
  115. }

2015ACM/ICPC亚洲区沈阳站的更多相关文章

  1. 2015ACM/ICPC亚洲区沈阳站 Pagodas

    Pagodas Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  2. 2015ACM/ICPC亚洲区沈阳站 B-Bazinga

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  3. hdu 5510 Bazinga (kmp+dfs剪枝) 2015ACM/ICPC亚洲区沈阳站-重现赛(感谢东北大学)

    废话: 这道题很是花了我一番功夫.首先,我不会kmp算法,还专门学了一下这个算法.其次,即使会用kmp,但是如果暴力枚举的话,还是毫无疑问会爆掉.因此在dfs的基础上加上两次剪枝解决了这道题. 题意: ...

  4. 2015ACM/ICPC亚洲区沈阳站 Solution

    A - Pattern String 留坑. B - Bazinga 题意:找一个最大的i,使得前i - 1个字符串中至少不是它的子串 思路:暴力找,如果有一个串已经符合条件,就不用往上更新 #inc ...

  5. 2015ACM/ICPC亚洲区沈阳站 部分题解

    链接在这:http://bak.vjudge.net/contest/132442#overview. A题,给出a,b和n,初始的集合中有a和b,每次都可以从集合中选择不同的两个,相加或者相减,得到 ...

  6. 2015ACM/ICPC亚洲区沈阳站重现赛-HDU5512-Pagodas-gcd

    n pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, l ...

  7. 2015ACM/ICPC亚洲区沈阳站-重现赛 M - Meeting (特殊建边,最短路)

    题意:有\(n\)个点,\(m\)个集合,集合\(E_i\)中的点都与集合中的其它点有一条边权为\(t_i\)的边,现在问第\(1\)个点和第\(n\)个点到某个点的路径最短,输出最短路径和目标点,如 ...

  8. 2015ACM/ICPC亚洲区沈阳站-重现赛 B - Bazinga (KMP)

    题意:给你\(n\)个字符串,\(s_1,s_2,...,s_n\),对于\(i(1\le i\le n)\),找到最大的\(i\),并且满足\(s_j(1\le j<i)\)不是\(s_i\) ...

  9. 2015ACM/ICPC亚洲区沈阳站-重现赛 D - Pagodas

    题意:有\(n\)个数,开始给你两个数\(a\)和\(b\),每次找一个没出现过的数\(i\),要求满足\(i=j+k\)或\(i=j-k\),当某个人没有数可以选的时候判他输,问谁赢. 题解:对于\ ...

随机推荐

  1. DropDownList控件

    1.DropDownList控件 <asp:DropDownList runat="server" ID="DropDownList1" AutoPost ...

  2. php.ini 中文注释

    这个文件控制了PHP许多方面的观点.为了让PHP读取这个文件,它必须被命名为 ; ´php.ini´.PHP 将在这些地方依次查找该文件:当前工作目录:环境变量PHPRC ; 指明的路径:编译时指定的 ...

  3. yii2 session的使用方法

    yii2打开session use yii\web\Session; $session = Yii::$app->session; // check if a session is alread ...

  4. Java编程中的美好

    java程序员如何写出"优美"代码,动力节点告诉你怎么办: 1.注释尽可能全面 对于方法的注释应该包含详细的入参和结果说明,有异常抛出的情况也要详细叙述:类的注释应该包含类的功能说 ...

  5. 10月30日上午MySQL数据库的修改(从网页上实现对数据库的更改)

    从网页页面上对数据库进行更改,连接着之前做的增加.删除.查询. 1.先做一个修改页面 <body> <!--这个页面需要让用户看到一些数据,所以不是一个纯php页面,页面效果和增加页 ...

  6. diff 比较两个文件的差异

    功能:比较两个文件的差异,并把不同地方的信息显示出来.默认diff格式的信息. diff比较两个文件或文件集合的差异,并记录下来,生成一个diff文件,这也是我们常说的补丁文件.也使用patch命令对 ...

  7. c# 网络

    http://www.cnblogs.com/fuchongjundream/p/4079128.html http://stackoverflow.com/questions/21728773/th ...

  8. golang笔记——流程控制

    条件语句 if ... else if ... else 语句,如: { fmt.Println(">100") } < num { fmt.Println(" ...

  9. Android检测网络是否正常代码!

    在Android开发中,如果该应用程序需要连接网络请求,那么最好我们先做一个检测网络是否在线的判断,否则程序容易出现卡死或FC等Bug,应该判断如果手机离线则弹出提示让用户检查网络,如果正常则继续执行 ...

  10. 02OC的类和对象

    这章重点介绍OC的类以及对象,由于C语言是面向过程语言,而OC只是对于C语言多了一些面向对象的特性,所以OC相对于其他面向对象语言,例如C#.Java等没有那么多的语法特性,所以差别还是比较大的. 一 ...