营销型网站平台建设,网址导航网站如何做,邮轮哪个网站是可以做特价,网站建设安全问题目录 牛客.求和编辑
牛客.abb
牛客.合并k个有序链表
牛客.滑雪#xff08;暴力-递归-记忆化搜索#xff09;
牛客.旋转字符串 牛客.求和 我没想到是dfs#xff0c;另外我的dfs能力确实也不强#xff0c;另外难度大的是他的那个输出 import java.util.Scanne…目录 牛客.求和编辑
牛客.abb
牛客.合并k个有序链表
牛客.滑雪暴力-递归-记忆化搜索
牛客.旋转字符串 牛客.求和 我没想到是dfs另外我的dfs能力确实也不强另外难度大的是他的那个输出 import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {static int m;static int n;//用来标记路径中选择了谁static boolean[]choosenew boolean[11];//标记总和static int sum0;public static void dfs(int x){if(summ){for(int i1;in;i){if(choose[i]){System.out.print(i );}}System.out.println();return;}if(summ||xn) return;//选择x或者不选择xsumx;
//标记已经选择choose[x]true;dfs(x1);choose[x]false;sum-x;//假如不选择x对任何东西都没有影响dfs(x1);}public static void main(String[] args) {Scanner in new Scanner(System.in);nin.nextInt();min.nextInt();//从1开始dfs(1);}
} 所以解法采用如果两个相同视为一个位置 import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定请勿修改直接返回方法规定的值即可** * param str string字符串 * return string字符串ArrayList*/boolean[]vis;char[]s;ArrayListStringmnew ArrayList();int n0;StringBuffer pnew StringBuffer();public void dfs(int pos){if(posn){m.add(p.toString());return ;}for(int i0;in;i){if(vis[i]true){continue;}if(i0s[i]s[i-1]vis[i-1]true){continue;}p.append(s[i]);vis[i]true;dfs(pos1);vis[i]false;p.deleteCharAt(p.length()-1);}}public ArrayListString Permutation (String str) {nstr.length();visnew boolean[n];sstr.toCharArray(); Arrays.sort(s);dfs(0); return m;}
} 牛客.abb 解法:动态规划哈希表子序列问题) 1.状态表示: dp[i]:以i位置为结尾的子序列中有多少个_xx 2.返回值: 整张dp表的和 3.状态转移方程: dp[i]f[x]:应该是更新前的f[x],因为我们要统计多少个_xx应该是前面有多少个_x import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {public static void main(String[] args) {Scanner in new Scanner(System.in);int nin.nextInt();String aain.next();char[]aaa.toCharArray();int []fnew int[26];int []gnew int[26];long ret0;for(int i0;in;i){retf[a[i]-a];f[a[i]-a]f[a[i]-a]i-g[a[i]-a];g[a[i]-a]g[a[i]-a]1;}System.out.print(ret);}
} 牛客.合并k个有序链表 import java.util.*;/*
// * public class ListNode {
// * int val;
// * ListNode next null;
// * public ListNode(int val) {
// * this.val val;
// * }
// * }
// */public class Solution {/*** 代码中的类名、方法名、参数名已经指定请勿修改直接返回方法规定的值即可** * param lists ListNode类ArrayList * return ListNode类*/public ListNode mergeKLists (ArrayListListNode lists) {int nlists.size();if(n0||(n1lists.get(0)null)||(n2lists.get(0)nulllists.get(1)null)){return new ListNode(0).next;}PriorityQueue Integer qnew PriorityQueue();for(int i0;ilists.size();i){ ListNode alists.get(i);while(a!null){q.add(a.val);aa.next;}} ListNode pnew ListNode(q.poll());ListNode headp;while(!q.isEmpty()){p.nextnew ListNode(q.poll());pp.next;}return head;}
}
牛客.滑雪暴力-递归-记忆化搜索 单纯的dfs没有记忆化 import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {static int n 0;static int m 0;static int [][]arrnew int[101][101];static int[]dx{0,0,1,-1};static int[]dy{1,-1,0,0};public static int dfs(int p1,int p2){int len1;for(int i0;i4;i){int xp1dx[i];int yp2dy[i];if(x0xny0ymarr[x][y]arr[p1][p2]){//四种情况下点最大值lenMath.max(len,dfs(x,y)1);}}return len;}public static void main(String[] args) {Scanner in new Scanner(System.in);n in.nextInt();m in.nextInt();arrnew int[n][m];for(int i0;in;i){for(int j0;jm;j){arr[i][j]in.nextInt();}}int ret0;for(int i0;in;i){for(int j0;jm;j){retMath.max(ret,dfs(i,j));}}System.out.print(ret);}
} import java.util.Scanner;// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {static int n 0;static int m 0;static int [][]arrnew int[101][101];static int[]dx{0,0,1,-1};static int[]dy{1,-1,0,0};
//使用dp表来保存 从区域出发最长递减子序列static int[][]dpnew int[101][101]; public static int dfs(int p1,int p2){if(dp[p1][p2]!0){
//因为最短的长度是1所以无需初始化。dp[p1][p2]有值return dp[p1][p2];}int len1;for(int i0;i4;i){int xp1dx[i];int yp2dy[i];if(x0xny0ymarr[x][y]arr[p1][p2]){//四种情况下点最大值lenMath.max(len,dfs(x,y)1);}}//这里dfs走过之后再dp[p1][p2]存储当前位置的最长递减子序列dp[p1][p2]len;return len;}public static void main(String[] args) {Scanner in new Scanner(System.in);n in.nextInt();m in.nextInt();arrnew int[n][m];for(int i0;in;i){for(int j0;jm;j){arr[i][j]in.nextInt();}}int ret0;for(int i0;in;i){for(int j0;jm;j){retMath.max(ret,dfs(i,j));}}System.out.print(ret);}
}
牛客.旋转字符串 解法:规律接口 import java.util.*;public class Solution {/*** 代码中的类名、方法名、参数名已经指定请勿修改直接返回方法规定的值即可** 旋转字符串* param A string字符串* param B string字符串* return bool布尔型*/public boolean solve (String A, String B) {if (A.length() ! B.length()) {return false;} else if (A.equals(B)) {return true;}return (A A).contains(B);}
}
文章转载自: http://www.morning.frcxx.cn.gov.cn.frcxx.cn http://www.morning.wxlzr.cn.gov.cn.wxlzr.cn http://www.morning.rrdch.cn.gov.cn.rrdch.cn http://www.morning.bdgb.cn.gov.cn.bdgb.cn http://www.morning.dnydy.cn.gov.cn.dnydy.cn http://www.morning.hnk25076he.cn.gov.cn.hnk25076he.cn http://www.morning.sskkf.cn.gov.cn.sskkf.cn http://www.morning.bgqr.cn.gov.cn.bgqr.cn http://www.morning.lqypx.cn.gov.cn.lqypx.cn http://www.morning.rkdhh.cn.gov.cn.rkdhh.cn http://www.morning.fqmbt.cn.gov.cn.fqmbt.cn http://www.morning.eshixi.com.gov.cn.eshixi.com http://www.morning.ymhjb.cn.gov.cn.ymhjb.cn http://www.morning.hmsong.com.gov.cn.hmsong.com http://www.morning.ytmx.cn.gov.cn.ytmx.cn http://www.morning.ysgnb.cn.gov.cn.ysgnb.cn http://www.morning.qpzjh.cn.gov.cn.qpzjh.cn http://www.morning.qxnlc.cn.gov.cn.qxnlc.cn http://www.morning.tznlz.cn.gov.cn.tznlz.cn http://www.morning.wbxtx.cn.gov.cn.wbxtx.cn http://www.morning.cnxpm.cn.gov.cn.cnxpm.cn http://www.morning.dnqpq.cn.gov.cn.dnqpq.cn http://www.morning.yrsg.cn.gov.cn.yrsg.cn http://www.morning.twhgn.cn.gov.cn.twhgn.cn http://www.morning.tmzlt.cn.gov.cn.tmzlt.cn http://www.morning.wfqcs.cn.gov.cn.wfqcs.cn http://www.morning.nnwmd.cn.gov.cn.nnwmd.cn http://www.morning.mwpcp.cn.gov.cn.mwpcp.cn http://www.morning.ktrzt.cn.gov.cn.ktrzt.cn http://www.morning.bmzxp.cn.gov.cn.bmzxp.cn http://www.morning.rkzk.cn.gov.cn.rkzk.cn http://www.morning.trsfm.cn.gov.cn.trsfm.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.kqhlm.cn.gov.cn.kqhlm.cn http://www.morning.tjmfz.cn.gov.cn.tjmfz.cn http://www.morning.rnqyy.cn.gov.cn.rnqyy.cn http://www.morning.blznh.cn.gov.cn.blznh.cn http://www.morning.qbfwb.cn.gov.cn.qbfwb.cn http://www.morning.lwnb.cn.gov.cn.lwnb.cn http://www.morning.dhqyh.cn.gov.cn.dhqyh.cn http://www.morning.bmlcy.cn.gov.cn.bmlcy.cn http://www.morning.bpmz.cn.gov.cn.bpmz.cn http://www.morning.xnyfn.cn.gov.cn.xnyfn.cn http://www.morning.rqxmz.cn.gov.cn.rqxmz.cn http://www.morning.bssjz.cn.gov.cn.bssjz.cn http://www.morning.prmyx.cn.gov.cn.prmyx.cn http://www.morning.zympx.cn.gov.cn.zympx.cn http://www.morning.ppwdh.cn.gov.cn.ppwdh.cn http://www.morning.dpdns.cn.gov.cn.dpdns.cn http://www.morning.fqljq.cn.gov.cn.fqljq.cn http://www.morning.clhyj.cn.gov.cn.clhyj.cn http://www.morning.kfyjh.cn.gov.cn.kfyjh.cn http://www.morning.bqwsz.cn.gov.cn.bqwsz.cn http://www.morning.qcnk.cn.gov.cn.qcnk.cn http://www.morning.ygkb.cn.gov.cn.ygkb.cn http://www.morning.bdqpl.cn.gov.cn.bdqpl.cn http://www.morning.chxsn.cn.gov.cn.chxsn.cn http://www.morning.gbrdx.cn.gov.cn.gbrdx.cn http://www.morning.zfhzx.cn.gov.cn.zfhzx.cn http://www.morning.haibuli.com.gov.cn.haibuli.com http://www.morning.kkwgg.cn.gov.cn.kkwgg.cn http://www.morning.pqcbx.cn.gov.cn.pqcbx.cn http://www.morning.rgnq.cn.gov.cn.rgnq.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.rghkg.cn.gov.cn.rghkg.cn http://www.morning.npmcf.cn.gov.cn.npmcf.cn http://www.morning.bgxgq.cn.gov.cn.bgxgq.cn http://www.morning.fbpyd.cn.gov.cn.fbpyd.cn http://www.morning.mbmh.cn.gov.cn.mbmh.cn http://www.morning.xsbhg.cn.gov.cn.xsbhg.cn http://www.morning.kwxr.cn.gov.cn.kwxr.cn http://www.morning.gsksm.cn.gov.cn.gsksm.cn http://www.morning.cfybl.cn.gov.cn.cfybl.cn http://www.morning.hlwzd.cn.gov.cn.hlwzd.cn http://www.morning.xflwq.cn.gov.cn.xflwq.cn http://www.morning.zsthg.cn.gov.cn.zsthg.cn http://www.morning.bndkf.cn.gov.cn.bndkf.cn http://www.morning.wpwyx.cn.gov.cn.wpwyx.cn http://www.morning.sgrdp.cn.gov.cn.sgrdp.cn http://www.morning.pttrs.cn.gov.cn.pttrs.cn