problem1 link

直接模拟。

import java.util.*;
import java.math.*;
import static java.lang.Math.*;public class Arrows {public int longestArrow(String s) {int result=-1;for(int i=0;i<s.length();++i) {final char c=s.charAt(i);if(c=='<') {int len=1;if(i+1<s.length()&&(s.charAt(i+1)=='-'||s.charAt(i+1)=='=')) {++len;for(int k=i+2;k<s.length()&&s.charAt(k)==s.charAt(i+1);++k) {++len;}}result=Math.max(result,len);}else if(c=='>') {int len=1;if(i-1>=0&&(s.charAt(i-1)=='-'||s.charAt(i-1)=='=')) {++len;for(int k=i-2;k>=0&&s.charAt(k)==s.charAt(i-1);--k) {++len;}}result=Math.max(result,len);}}return result;}
}

problem2 link

把词建一个树,那么对于一个节点$u$,如果$u$不是一个单词,那么$f(u)=\prod _{v\in S_{u}}f(v)$,否则$f(u)=1+\prod _{v\in S_{u}}f(v)$.$S_{u}$表示$u$的孩子集合。

import java.util.*;
import java.math.*;
import static java.lang.Math.*;public class PrefixFreeSubsets {static class Tree {public boolean end;Tree[] sons=null;public Tree() {end=false;sons=new Tree[26];}}static Tree head=null;public long cantPrefFreeSubsets(String[] words) {head=new Tree();for(String s:words) {Tree cur=head;for(int i=0;i<s.length();++i) {int t=s.charAt(i)-'a';if(cur.sons[t]==null) {cur.sons[t]=new Tree();}cur=cur.sons[t];}cur.end=true;}return dfs(head);}static long dfs(Tree cur) {long result=1,num=0;for(int i=0;i<26;++i) {if(cur.sons[i]==null) {continue;}result*=dfs(cur.sons[i]);++num;}if(num==0) {return 2;}if(cur.end) {++result;}return result;}
}

problem3 link

设$f[i]=1$ 表示先手赢,$f[i]=0$ 表示后手赢。由于每次拿的石子数范围是$[1,22]$,所以如果出现两个位置$p,q$,使得$f[p],f[p+1],..,f[p+21]$与$f[q],f[q+1],..,f[q+21]$完全相等,那么就形成一个循环。所以只需要暴力找到这个循环位置即可。

import java.util.*;
import java.math.*;
import static java.lang.Math.*;public class LongLongNim {static boolean[] f=new boolean[1<<22];static int[] g=new int[1<<22];public int numberOfWins(int maxN,int[] moves) {Arrays.fill(f,false);Arrays.fill(g,-1);f[0]=false;int pre=0;int start0=-1,start1=-1;for(int i=1;i<=maxN;++i) {boolean ok=false;for(int j=0;j<moves.length&&i>=moves[j];++j) {if(!f[i-moves[j]]) {ok=true;break;}}f[i]=ok;if(f[i]) {pre=pre<<1|1;}else {pre=pre<<1;}if(i>22&&(pre&(1<<22))!=0) {pre^=1<<22;}if(i<22) {continue;}if(g[pre]==-1) {g[pre]=i-21;}else if(i-21-g[pre]>=22){start0=g[pre];start1=i-21;break;}}if(start0==-1) {int result=0;for(int i=1;i<=maxN;++i) {if(!f[i]) {++result;}}return result;}int result0=0,result1=0;for(int i=1;i<start0;++i) {if(!f[i]) {++result0;}}for(int i=start0;i<start1;++i) {if(!f[i]) {++result1;}}maxN-=start0-1;result1*=maxN/(start1-start0);maxN%=start1-start0;for(int i=0;i<maxN;++i) {if(!f[start0+i]) {++result1;}}result1+=result0;return result1;}
}

  

转载于:https://www.cnblogs.com/jianglangcaijin/p/7441289.html

topcoder srm 330 div1相关推荐

  1. topcoder srm 714 div1

    problem1 link 倒着想.每次添加一个右括号再添加一个左括号,直到还原.那么每次的右括号的选择范围为当前左括号后面的右括号减去后面已经使用的右括号. problem2 link 令$h(x) ...

  2. topcoder srm 691 div1 -3

    1.给定一个$n$个顶点$n$个边的图,边是$(i,a_{i})$,顶点编号$[0,n-1]$.增加一个顶点$n$,现在选出一个顶点集$M$,对于任意的在$M$中 的顶点$x$,去掉边$(x,a_{x ...

  3. topcoder srm 706 div1

    1.给定一个迷宫,点号表示不可行,井号表示可行.现在可以改变其中的一些井号的位置.问最少改变多少个井号可以使得从左上角到右下角存在路径. 思路:设高为$n$,宽为$m$,若井号的个数$S$小于$n+m ...

  4. topcoder srm 694 div1 -3

    1.给出$n$个数字,将其分成三个非空的组,每组的权值为该组所有数字的抑或.选择一种分法使得三组的权值和最大? 思路:记录前两组的权值且三组有没有数字时第三组的值.(当前两组的值知道时第三组的权值是确 ...

  5. topcoder srm 360 div1

    problem1 link (1)$n \neq m$时,假设$n<m$,那么同一行中的$m$个数字必定都相等. (2)$n=m$时,要满足任意的$i_{1},i_{2},j_{1},j_{2} ...

  6. topcoder srm 635 div1

    problem1 link 首先枚举长度$L$.然后计算每一段长度$L$的差值最大公约数,然后差值除以最大公约数的结果可以作为当前段的关键字.然后不同段就可以比较他们的关键字,一样就是可以转化的. p ...

  7. topcoder srm 495 div1

    problem1 link 从前向后确定一下,然后再从后向前确定一下.一样的话就是可以确定的. problem2 link 首先将强连通分量缩点.理论上来说,只需要遍历所有入度为0的联通块中的一个即可 ...

  8. topcoder srm 325 div1

    problem1 link $g[i]$表示解决前$i$个的代价,那么$g[i]$是所有$g[j]+cost(j+1,i)$的最小值. import java.util.*; import java. ...

  9. topcoder srm 500 div1

    problem1 link 如果decisions的大小为0,那么每一轮都是$N$个人.答案为0. 否则,如果答案不为0,那么概率最大的一定是一开始票数最多的人.因为这个人每一轮都在可以留下来的人群中 ...

最新文章

  1. iOS 设置button文字过长而显示省略号的解决办法
  2. java调用ole ie_ActiveX (.ocx)的写法,及在IE里调用
  3. 机器学习性能评估指标(综合性总结)
  4. [攻防世界][CTF][2020][MISC] 攻防世界 MISC writeup
  5. weex android app例子,weex中修改android app图标和欢迎页
  6. Android studio Mac 版上传代码提示The subversion command line tools are no longer provided by Xcode
  7. java定时器注解加效验_Java中定时器的使用之二(springboot–@Scheduled注解)
  8. h5-localStorage实现缓存ajax请求数据
  9. ASP.NET会话(Session)模式
  10. java.lang.ExceptionInInitializerError异常
  11. [JavaScript 刷题] 树 - 将有序数组转换为二叉搜索树, leetcode 108
  12. adb install -r -d的含义
  13. 微信营销为什么会用到微信公众号客服系统?
  14. 一名3年工作经验的程序员应该具备的技能 !
  15. win7安装vmware+ubuntu16.04
  16. 详解AUTOSAR:AUTOSAR方法论(理论篇—3)
  17. OnlyOffice官方Demo必要的修改及参数解释
  18. python中元组拆包_Python 元组拆包和具名元组解析
  19. UMLChina建模竞赛第3赛季第8轮:交友辅助系统,赵雅芝
  20. Flutter如何使widget始终保持在屏幕底部

热门文章

  1. Unity Shader 阴影
  2. NHibernate Step by Step:序篇 (转)
  3. InnoDB,5项实践
  4. 《数字质量手册》新书问答
  5. 无线路由器配置不佳 可耗净手机电量
  6. Spring全局异常处理的三种方式
  7. 从Netty到EPollSelectorImpl学习Java NIO
  8. 程序提示确认关闭后退出
  9. Install pysnmp for django
  10. 解决Lync Server 2013无法共享演示PPT