Div4 898 G,H

文章讲述了如何处理一个字符串问题,通过分析连续字符并计算变化后的数量,以及在一个环形结构中寻找最短路径以实现逃离。还涉及到Dijkstra算法的应用。
摘要由CSDN通过智能技术生成

Here

G. ABBC or BACB

 

 

解题思路

  • 一个B可以向左或向右吃掉一段连续的A
  • 将连续的A合成一个
  • 则字符串变为每个A之间被B隔开
  • \left\{\begin{matrix} B\cdots AB\cdots A\\ AB\cdots AB\cdots A \\ B\cdots AB\cdots AB\\ AB\cdots AB\cdots\ AB\\\cdots =BBB\cdots \end{matrix}\right.
  • 统计变化后AB的数量为numA,numB
  • numA\leq numB,则ans=SumA
  • numA>numB,则只会大1,即有一段A没被吃掉,则让长度最小的剩下,ans=SumA-mi
  • 省略号间的B的个数不影响答案,删去只留两个也一样,ABABB=ABAB,numB\geq numA

import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;













//implements Runnable
public class Main {
	static long md=(long)998244353;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	static int N=2000010;
	static int n=0;
	static int m=0;
	
	static void solve() throws Exception{
		AReader input=new AReader();
//		Scanner input=new Scanner(System.in);
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));	
		String al="abcdefghijklmnopqrstuvwxyz";
		char[] ac=al.toCharArray();

		int T=input.nextInt();
		while(T>0) {
			T--;	
			String string=" "+input.next();
			char[] s=string.toCharArray();
			n=string.length()-1;
			int[] b=new int[n+10];
			Arrays.fill(b, -1);
			int bb=0;
			int[] c=new int[n+10];
			int numa=0;
			int numb=0;
			for(int i=1;i<=n;++i) {
				if(s[i]!=s[i-1]) {
					bb++;
					b[bb]=s[i]-'A';
					c[bb]=1;
					if(s[i]=='B')numb++;
					else numa++;
				}else if(s[i]=='B') {
					bb++;
					b[bb]=s[i]-'A';
					c[bb]=1;
					numb++;
				}else c[bb]++;
			}
			if(numa<=numb) {
				long ans=0;
				for(int i=1;i<=bb;++i) {
					if(b[i]==0)ans+=c[i];
				}
				out.println(ans);
			}else {
				long mi=Linf;
				long ans=0;
				for(int i=1;i<=bb;++i) {
					if(b[i]==0) {
						mi=Math.min(mi, c[i]);
						ans+=c[i];
					}
				}
				if(mi==Linf)mi=0;
				ans-=mi;
				out.println(ans);
			}
 		}
		
	    out.flush(); 
	    out.close();
	}
	public static void main(String[] args) throws Exception{
		solve();
	}
//	public static final void main(String[] args) throws Exception {
//		  new Thread(null, new Tx2(), "线程名字", 1 << 27).start();
//	}
//		@Override
//		public void run() {
//			try {
//				//原本main函数的内容
//				solve();
//
//			} catch (Exception e) {
//			}
//		}
		static
		class AReader{ 
		    BufferedReader bf;
		    StringTokenizer st;
		    BufferedWriter bw;

		    public AReader(){
		        bf=new BufferedReader(new InputStreamReader(System.in));
		        st=new StringTokenizer("");
		        bw=new BufferedWriter(new OutputStreamWriter(System.out));
		    }
		    public String nextLine() throws IOException{
		        return bf.readLine();
		    }
		    public String next() throws IOException{
		        while(!st.hasMoreTokens()){
		            st=new StringTokenizer(bf.readLine());
		        }
		        return st.nextToken();
		    }
		    public char nextChar() throws IOException{
		        //确定下一个token只有一个字符的时候再用
		        return next().charAt(0);
		    }
		    public int nextInt() throws IOException{
		        return Integer.parseInt(next());
		    }
		    public long nextLong() throws IOException{
		        return Long.parseLong(next());
		    }
		    public double nextDouble() throws IOException{
		        return Double.parseDouble(next());
		    }
		    public float nextFloat() throws IOException{
		        return Float.parseFloat(next());
		    }
		    public byte nextByte() throws IOException{
		        return Byte.parseByte(next());
		    }
		    public short nextShort() throws IOException{
		        return Short.parseShort(next());
		    }
		    public BigInteger nextBigInteger() throws IOException{
		        return new BigInteger(next());
		    }
		    public void println() throws IOException {
		        bw.newLine();
		    }
		    public void println(int[] arr) throws IOException{
		        for (int value : arr) {
		            bw.write(value + " ");
		        }
		        println();
		    }
		    public void println(int l, int r, int[] arr) throws IOException{
		        for (int i = l; i <= r; i ++) {
		            bw.write(arr[i] + " ");
		        }
		        println();
		    }
		    public void println(int a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(int a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(String a) throws IOException{
		        bw.write(a);
		        bw.newLine();
		    }
		    public void print(String a) throws IOException{
		        bw.write(a);
		    }
		    public void println(long a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(long a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(double a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(double a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void print(char a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(char a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		}
	}

		

	

H. Mad City

 

解题思路

  • n个点n条边,构成一个只有一个环,其余边形成挂在环上的链
  • 考虑什么时候可以无限逃离
  • b被逮到前跑到环上
  •  b通过其所在的链到环上,所以a可能在b到环之前跑到b链与环的接点处,守株待兔
  • 通过从bDFS找到接点s(第一个被访问两次) 
  • 然后从s跑最短路,判断a,b到其距离
  • dis_a>dis_b,则b无限,反之,则寄

import java.io.*;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;













//implements Runnable
public class Main {
	static long md=(long)998244353;
	static long Linf=Long.MAX_VALUE/2;
	static int inf=Integer.MAX_VALUE/2;
	static int N=2000010;
	static int n=0;
	static int m=0;
	
	static
	class Node{
		long x;
		long y;
		public Node() {
			
		}
		public Node(long u,long v) {
			x=u;
			y=v;
		}
		@Override
	    public boolean equals(Object o) {
	        if (this == o) return true;
	        if (o == null || getClass() != o.getClass()) return false;
	        Node now = (Node) o;
	        return x==now.x&&y==now.y;
	    }

	    @Override
	    public int hashCode() {
	        return Objects.hash(x, y);
	    }
	}
	static
	class Edge{
		int fr,to,nxt;
		public Edge(int u,int v) {
			fr=u;
			to=v;
		}
	}
	static Edge[] e;
	static int[] head;
	static int cnt=0;
	static void addEdge(int fr,int to) {
		cnt++;
		e[cnt]=new Edge(fr,to);
		e[cnt].nxt=head[fr];
		head[fr]=cnt;
	}
	static boolean Dij(int s,int a,int b) {
		long[] dis=new long[n+1];
		Arrays.fill(dis, Linf);
		boolean[] vis=new boolean[n+1];
		PriorityQueue<Node> q=new PriorityQueue<Node>((o1,o2)->{
			if(o1.y-o2.y>0)return 1;
			else if(o1.y-o2.y<0)return -1;
			else return 0;
		});
		q.add(new Node(s,0));
		dis[s]=0;
		while(!q.isEmpty()) {
			Node now=q.peek();q.poll();
			int x=(int)now.x;
			if(vis[x])continue;
			vis[x]=true;
			for(int i=head[x];i>0;i=e[i].nxt) {
				int v=e[i].to;
				if(dis[v]>dis[x]+1) {
					dis[v]=dis[x]+1;
					q.add(new Node(v,dis[v]));
				}
			}
		}
		if(dis[b]<dis[a])return true;
		else return false;
	}
	static boolean[] op;
	static int findrt(int x,int fa) {
		if(op[x])return x;
		op[x]=true;
		for(int i=head[x];i>0;i=e[i].nxt) {
			int v=e[i].to;
			if(v==fa)continue;
			int may=findrt(v, x);
			if(may!=0)return may;
		}
		return 0;
	}
	static void solve() throws Exception{
		AReader input=new AReader();
//		Scanner input=new Scanner(System.in);
		PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));	
		String al="abcdefghijklmnopqrstuvwxyz";
		char[] ac=al.toCharArray();
		
		int T=input.nextInt();
		while(T>0) {
			T--;	
			n=input.nextInt();
			int a=input.nextInt();
			int b=input.nextInt();
			e=new Edge[2*n+5];
			head=new int[n+1];
			cnt=0;
			op=new boolean[n+1];
			
			for(int i=1;i<=n;++i) {
				int u=input.nextInt();
				int v=input.nextInt();
				addEdge(u, v);
				addEdge(v, u);
			}
			int s=findrt(b, 0);
			if(Dij(s, a, b))out.println("YES");
			else out.println("NO");
 		}
		
	    out.flush(); 
	    out.close();
	}
	public static void main(String[] args) throws Exception{
		solve();
	}
//	public static final void main(String[] args) throws Exception {
//		  new Thread(null, new Tx2(), "线程名字", 1 << 27).start();
//	}
//		@Override
//		public void run() {
//			try {
//				//原本main函数的内容
//				solve();
//
//			} catch (Exception e) {
//			}
//		}
		static
		class AReader{ 
		    BufferedReader bf;
		    StringTokenizer st;
		    BufferedWriter bw;

		    public AReader(){
		        bf=new BufferedReader(new InputStreamReader(System.in));
		        st=new StringTokenizer("");
		        bw=new BufferedWriter(new OutputStreamWriter(System.out));
		    }
		    public String nextLine() throws IOException{
		        return bf.readLine();
		    }
		    public String next() throws IOException{
		        while(!st.hasMoreTokens()){
		            st=new StringTokenizer(bf.readLine());
		        }
		        return st.nextToken();
		    }
		    public char nextChar() throws IOException{
		        //确定下一个token只有一个字符的时候再用
		        return next().charAt(0);
		    }
		    public int nextInt() throws IOException{
		        return Integer.parseInt(next());
		    }
		    public long nextLong() throws IOException{
		        return Long.parseLong(next());
		    }
		    public double nextDouble() throws IOException{
		        return Double.parseDouble(next());
		    }
		    public float nextFloat() throws IOException{
		        return Float.parseFloat(next());
		    }
		    public byte nextByte() throws IOException{
		        return Byte.parseByte(next());
		    }
		    public short nextShort() throws IOException{
		        return Short.parseShort(next());
		    }
		    public BigInteger nextBigInteger() throws IOException{
		        return new BigInteger(next());
		    }
		    public void println() throws IOException {
		        bw.newLine();
		    }
		    public void println(int[] arr) throws IOException{
		        for (int value : arr) {
		            bw.write(value + " ");
		        }
		        println();
		    }
		    public void println(int l, int r, int[] arr) throws IOException{
		        for (int i = l; i <= r; i ++) {
		            bw.write(arr[i] + " ");
		        }
		        println();
		    }
		    public void println(int a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(int a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(String a) throws IOException{
		        bw.write(a);
		        bw.newLine();
		    }
		    public void print(String a) throws IOException{
		        bw.write(a);
		    }
		    public void println(long a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(long a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(double a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		    public void print(double a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void print(char a) throws IOException{
		        bw.write(String.valueOf(a));
		    }
		    public void println(char a) throws IOException{
		        bw.write(String.valueOf(a));
		        bw.newLine();
		    }
		}
	}

		

	

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值