首页 > 试题广场 >

推倒吧骨牌

[编程题]推倒吧骨牌
  • 热度指数:4621 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解

输入描述:
输入为一个长度不超过1000的,仅包含‘L’,‘R’,‘.’的字符串


输出描述:
根据输入,输出一个仅由‘L’,‘R’,‘.’组成的结果字符串
示例1

输入

.L.R...LR....L.

输出

LL.RR.LLRRRLLL.
/*链接:https://www.nowcoder.com/questionTerminal/fae1307a24ae4e9ea852a646a4f812bf?answerType=1&f=discussion
来源:牛客网

这个字符串一共就分为四种情况:
(1):L...L ,在这种情况下,将里面‘.’全部换成'L';
(2):R...L,在这种情况下,将里面左半部分替换为'L',右半部分替换为'R',需要注意的是区间长度的奇偶问题
(3):R...R,在这种情况下,将里面‘.’全部换成'R';
(4):L...R,在这种情况下,我们不做任何处理;
备注:为了考虑左右开头的影响,我们分别在字符串的最左边加上“L”,最右边加上“R”,
这样既不会影响内部的判断,又能处理端点的情况
*/
import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        StringBuilder sb = new StringBuilder(s);//为了统一处理,
        sb.append("R");//在最右边加一个“R”
        sb.insert(0, "L");//在最左边加一个“L”
        List<Integer> v = new ArrayList<>();//记录含有L或R的下标位置
        for(int i = 0; i < sb.length(); ++i){
            if(sb.charAt(i) == 'L' || sb.charAt(i) == 'R')
                v.add(i);;
        }
        for(int p = 0; p < v.size() - 1; ++p){//按RL区间端点分类,共有4类
            int i = v.get(p), j = v.get(p + 1);//一个区间一个区间处理(区间都是相邻的处理)
            char c1 = sb.charAt(i), c2 = sb.charAt(j);
            if(c1 == 'L' && c2 == 'L'){
                for(int k = i + 1; k < j; ++k)
                    sb.setCharAt(k, 'L');
            }
            if(c1 == 'L' && c2 == 'R'){}//这种什么也不用做
            if(c1 == 'R' && c2 == 'L'){
                for(int k = i + 1; k < (double)(j + i)/2; ++k)
                    sb.setCharAt(k, 'R');
                for(int k = j - 1; k > (double)(j + i)/2; --k)
                    sb.setCharAt(k, 'L');
            }
            if(c1 == 'R' && c2 == 'R'){
                for(int k = i + 1; k < j; ++k)
                    sb.setCharAt(k, 'R');
            }
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.deleteCharAt(0);//把开始加的那两张去掉
        System.out.println(sb);
    }
}

发表于 2020-06-18 19:40:47 回复(0)
/*
LL LR RL RR
*/
import java.util.Scanner;

public class Main {
    public static void main (String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] in = sc.next().toCharArray();
        int len = in.length;
        char first = 'a';
        int firsti = 0;
        char second = 'a';
        int secondi = 0;
        int front = 0;
        int pair = 0;
        for (int i = 0; i < len; i++) {
            if (in[i] == '.') {
                continue;
            }
            else if (in[i] == 'L') {
                if (pair == 0) {
                    first = 'L';
                    firsti = i;
                    pair++;
                }
                else {
                    second = 'L';
                    secondi = i;
                    pair++;
                }
            }
            else {
                if (pair == 0) {
                    first = 'R';
                    firsti = i;
                    pair++;
                }
                else {
                    second = 'R';
                    secondi = i;
                    pair++;
                }
            }
            if (pair == 2) {
                if (first == 'L' && second == 'L') {
                    pair = 1;
                    for (int j = front; j < secondi; j++) {
                        System.out.print('L');
                    }
                    front = secondi;
                    firsti = secondi;
                }
                if (first == 'L' && second == 'R') {
                    pair = 1;
                    for (int j = front; j <= firsti; j++) {
                        System.out.print('L');
                    }
                    for (int j = firsti+1; j < secondi; j++) {
                        System.out.print('.');
                    }
                    front = secondi;
                    first = 'R';
                    firsti = secondi;
                }
                if (first == 'R' && second == 'L') {
                    pair = 1;
                    for (int j = front; j < firsti; j++) {
                        System.out.print('.');
                    }
                    for (int j = firsti; j < firsti+(secondi-firsti+1)/2; j++) {
                        System.out.print('R');
                    }
                    int s = firsti+(secondi-firsti+1)/2;
                    if ((secondi-firsti+1)%2 == 1) {
                        System.out.print('.');
                        s++;
                    }
                    for (int j = s; j < secondi; j++) {
                        System.out.print('L');
                    }
                    front = secondi;
                    first = 'L';
                    firsti = secondi;
                }
                if (first == 'R' && second == 'R') {
                    pair = 1;
                    for (int j = front; j < firsti; j++) {
                        System.out.print('.');
                    }
                    for (int j = firsti; j < secondi; j++) {
                        System.out.print('R');
                    }
                    front = secondi;
                    firsti = secondi;
                }
            }
        }
        if (first == 'L') {
            System.out.print('L');
            front++;
            for (int j = front; j < len; j++) {
                System.out.print('.');
            }
        }
        else {
            for (int j = front; j < len; j++) {
                System.out.print('R');
            }
        }
    }
}

发表于 2020-05-31 09:54:38 回复(0)
import java.util.Scanner;
//49 真繁琐
public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		char[] ch = sc.next().toCharArray();
		char status = '0';
		int index = 0;
		for(int i=0;i<ch.length;i++){
			if(ch[i]=='L'&& status=='0'){
				for(int j=i-1;j>=0;j--){
                    if(ch[j]!='.'){
                        break;
                    }
					ch[j]='L';
				}
				continue;
			}
			if(ch[i]=='L'&& status =='R'){
				if((i-index-1)%2!=0){
					int k = (i+index)/2;
					for(int j=index+1;j<i;j++){
						if(j<k){
							ch[j] = 'R';
						}else if(j>k){
							ch[j] = 'L';
						}
					}
				}else{
					int k = (i+index)/2;
					for(int j=index+1;j<i;j++){
						if(j<=k){
							ch[j] = 'R';
						}else{
							ch[j] = 'L';
						}
					}
				}
				status = '0';
				continue;
			}
            if(ch[i]=='R' && status=='R'){
                for(int j=i-1;j>index;j--){
                    ch[j]='R';
                }
                status = 'R';
                index = i;
                continue;
            }else if(ch[i]=='R'){
                status = 'R';
				index = i;
				continue;
            }
			if(i==ch.length-1 && status=='R'){
				for(int j=index+1;j<=i;j++){
                    if(ch[j]!='.'){
						break;
					}
					ch[j] = 'R';
				}
			}
			
		}
		for(char c:ch){
			System.out.print(c);
		}
	}
}

发表于 2019-09-14 20:36:27 回复(0)
从左到右,依次遍历
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.nextLine();
        char[] c = str.toCharArray();
        int i = -1;
        int j = 0;
        while (j < c.length){
            if (c[j] == '.')
                j++;
            else if (c[j] == 'L'){
                for (int k=i+1;k<j;k++){
                    c[k] = 'L';
                }
                i = j;
                j++;
            }else if (c[j] == 'R'){
                int k;
                for (k=j+1;k<c.length;k++){
                    if (c[k] != '.')
                        break;
                }
                j++;
                if (k == c.length){
                    while (j < c.length){
                        c[j] = 'R';
                        j++;
                    }
                }else if (c[k] == 'R'){
                    while (j < k){
                        c[j] = 'R';
                        j++;
                    }
                    i = k;
                }else if (c[k] == 'L'){
                    int t = k-1;
                    while (j < t){
                        c[j] = 'R';
                        c[t] = 'L';
                        j++;t--;
                    }
                    j = k+1;
                    i = k;
                }
            }
        }
        for (char ch : c)
            System.out.print(ch);
    }
}


发表于 2019-08-06 16:42:46 回复(0)

图片说明

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        char[] chs = scanner.next().toCharArray();
        int len = chs.length;
        int[] values = new int[len];

        // 第一遍从左往右遍历,遇到R就赋值length,接下来如果是.则递减,直到遇到L清零
        int value = 0;
        for (int i = 0; i < len; i++) {
            if (chs[i] == 'R') {
                value = len;
            } else if (chs[i] == 'L') {
                value = 0;
            } else {
                // 保证L之后遇到.仍旧赋值0而不是负数
                value = Math.max(value - 1, 0);
            }
            // values数组加上value值
            values[i] += value;
        }

        // 第二遍从右往左遍历,遇到L就赋值length,接下来如果是.则递减,直到遇到R清零
        value = 0;
        for (int i = len - 1; i >= 0; --i) {
            if (chs[i] == 'L') {
                value = len;
            } else if (chs[i] == 'R') {
                value = 0;
            } else {
                // 保证R之后遇到.仍旧赋值0而不是负数
                value = Math.max(value - 1, 0);
            }
            // values数组减去value值
            values[i] -= value;
        }

        StringBuilder result = new StringBuilder();
        // values值大于0则为R,小于0则为L,等于0则为.
        for (int i : values) {
            result.append(i > 0 ? 'R' : i < 0 ? 'L' : '.');
        }
        System.out.println(result);
    }
}

参考https://leetcode.com/articles/push-dominoes/#

编辑于 2019-07-02 19:39:26 回复(2)