赞
踩
String str = “((jhkv)(love)i)”; // 变为ilovejhkv
String str = “(u(s)(love)i)”; // 变为ilovesu
从里面括号到外面括号依次反转,然后拼接
思路用 递归
定义:inverse(str,l,r) 反转str从l到r不包括括号的字符串
若
package com.company; public class fanzhuan { public static void main(String[] args) { // String str = "((jhkv)(love)i)"; String str = "(u(s)(love)i)"; System.out.println(inverse(str, 0, str.length()-1)); } private static String inverse(String str, int l, int r) { if(r-l<=1) return ""; StringBuilder sb = new StringBuilder(); for(int i=l+1; i<r;i++) { char c = str.charAt(i); if(c != '(') sb.append(c); else { int j; for(j=i+1;i<r;j++) { if(str.charAt(j)==')') break; } sb.append(inverse(str,i,j)); i=j; } } return sb.reverse().toString(); } }
#include <iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; int main() { int N; cin >> N; vector<int> ve(N); int temp; for (int i = 0; i < N; i++) { cin >> temp; ve[i] = temp; } vector<int> ans; ans.push_back(ve[0]); int i = 1, lastspeed = ve[0],count=1,ABEfalg=0,ABEedge=0; while (i<N) { if (lastspeed - ve[i] >= 9) { ABEfalg++; } else { if (ABEfalg >= 4) { int l = max(ABEedge, i - ABEfalg-4); int r = min(N - 1, i + 3); for (int jj = l; jj <= r; jj++) ans.push_back(ve[jj]); ABEedge = i + 4; count = -3; } ABEfalg = 0; } if (count == 60&&ABEfalg<4) { ans.push_back(ve[i]); count = 0; } lastspeed = ve[i];i++; count++; } if (ABEfalg >= 4) { int l = max(ABEedge, i - ABEfalg - 4); int r = min(N - 1, i + 3); for (int jj = l; jj <= r; jj++) ans.push_back(ve[jj]); } int numcc = ans.size() - 1; for (int luo=0;luo<numcc;luo++) cout << ans[luo] << ','; cout << ans[numcc] << endl; return 0; }
4
1 3 1 1
第1个基站最多传到1+1=2
第2个基站最多传到2+3=5
最短1-2-4 最少2基站 1 2
1-2-3-4 要3个基站
用两个指针,i指针指向区间范围的最左边,r指针指向区间范围的最右边
当r指向最后一个基站n-1时结束
package com.company; public class jizhan { public static void main(String[] args) { // int[] a={2,5,1,1,1,1}; int[] a={1,3,1,1,2,1,3,1,1}; System.out.println(leastCount(a)); } private static int leastCount(int[] a) { int ans=1, r=a[0]; int lenp=0; for (int i = 0; i < a.length; ) { if(r>=a.length-1)break; for (int j = i+1; j <= r; j++) { if(j+a[j]>lenp+a[lenp]) lenp=j; } i=r; r=lenp+a[lenp]; ans++; } return ans; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。