首页 > 试题广场 >

彩色的砖块

[编程题]彩色的砖块
  • 热度指数:30814 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
小易有一些彩色的砖块。每种颜色由一个大写字母表示。各个颜色砖块看起来都完全一样。现在有一个给定的字符串s,s中每个字符代表小易的某个砖块的颜色。小易想把他所有的砖块排成一行。如果最多存在一对不同颜色的相邻砖块,那么这行砖块就很漂亮的。请你帮助小易计算有多少种方式将他所有砖块排成漂亮的一行。(如果两种方式所对应的砖块颜色序列是相同的,那么认为这两种方式是一样的。)
例如: s = "ABAB",那么小易有六种排列的结果:
"AABB","ABAB","ABBA","BAAB","BABA","BBAA"
其中只有"AABB"和"BBAA"满足最多只有一对不同颜色的相邻砖块。

输入描述:
输入包括一个字符串s,字符串s的长度length(1 ≤ length ≤ 50),s中的每一个字符都为一个大写字母(A到Z)。


输出描述:
输出一个整数,表示小易可以有多少种方式。
示例1

输入

ABAB

输出

2
思想就是看字符串里有几种字符,超过两种就不可能只有一对相邻的不同字符,有两种字符就是两种正确的排列,有一种字符自然就是一种正确的排列。
package sort;

import java.util.HashSet;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		String string = scanner.nextLine();
		// 用hashSet记录字符串中有几种字符
		HashSet<String> hashSet = new HashSet<>();
		for (int i = 0; i < string.length(); i++) {
			hashSet.add(string.substring(i, i + 1));
			// 如果一旦有两种以上的字符就输出0
			if (hashSet.size() > 2) {
				System.out.println(0);
				return;
			}
		}
		// 有两种字符就输出2,有一种字符就输出1
		System.out.println(hashSet.size());
	}
}

发表于 2017-08-13 10:05:35 回复(12)

python解法

水出天际的一道题目,刷新了认知下限。


思路

对每种颜色砖块个数做统计:

  1. 如果出现了两种以上的颜色,肯定最少有两种相邻,直接输出0.
  2. 如果出现了一种,那就只有一种。
  3. 如果出现了两种,那就只有两种。
from collections import Counter

arr = Counter(input()).values()
if len(arr) > 2:
    print(0)
elif len(arr) == 1:
    print(1)
else:
    print(2)
发表于 2019-02-24 18:55:47 回复(4)
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main()
{
	string str;
	int res = 0;
	cin >> str;
	sort(str.begin(), str.end());
	//cout << str << endl;
	str.erase(unique(str.begin(), str.end()),str.end());
	if (str.size() == 1)
		res = 1;
	else if (str.size() == 2)
		res = 2;
	cout << res << endl;
	return 0;
}

发表于 2017-08-12 22:13:46 回复(2)


s = raw_input()
if len(set(s)) == 2:
    print 2
elif len(set(s)) == 1:
    print 1
else:
    print 0
编辑于 2017-08-13 15:07:18 回复(4)

还以为多复杂 就是统计字符串中不同字符的数目 2个就是2种 3个以上0种 1个1种
不用骚操作原生hash代码如下

#include <iostream>
#include <string>
using namespace std;
int ha[30], cnt = 0, ans = 0;
string s;
int main(){
    cin>>s;
    for(int i = 0; i < s.length(); i++) {
        if(ha[s[i]-'A'] == 0) cnt++;
        ha[s[i]-'A']++;
    }
    if(cnt == 2) ans = 2; if(cnt == 1) ans = 1;
    cout<<ans<<endl;
    return 0;
}
发表于 2019-02-27 11:20:16 回复(0)
import java.util.Scanner;

public class wy1 {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String s = sc.nextLine();
        System.out.println(caculate(s));
    }

    /**
     * 思路:题目看着挺复杂,其实就是统计不同字符的个数。
     *
     * @param brick
     * @return
     */
    public static int caculate(String brick) {
        int character[] = new int[26];
        int count = 0;
        for (int i = 0; i < brick.length(); ++i) {
            if (character[(brick.charAt(i) - 'A')] == 0) {
                character[(brick.charAt(i) - 'A')] = 1;
                count++;
            }
            if (count > 2)
                return 0;
        }
        return count;
    }
}

发表于 2019-01-16 12:41:25 回复(0)
 #include<stdio.h>
#include<string.h>
int main()
{
    int i,j;
    char var;
    char arr[50];
    int count=0;
    for(i=0;i<50;i++)
    {
        scanf("%c",&arr[i]);
    }
    for(i=0;i<49;i++)
    {
        for(j=i+1;j<50;j++)
        {
            if(arr[i]>arr[j])
            {
                var=arr[j];
                arr[j]=arr[i];
                arr[i]=var;
            }
        }
    }    
    for(i=1;i<50;i++)
    {
        if(arr[i]-arr[i-1]!=0)
        {
            count++;
        }
    }
    if(count>1)
    {
        printf("0\n");
    }
    else
    {
        printf("%d\n",count+1);
    }
    return 0;
}

发表于 2017-08-25 16:12:03 回复(0)
import java.util.*;  public class Main { public static void main(String[] a) {
        Scanner in = new Scanner(System.in);  while (true) {
            String str = in.nextLine();  if (str.length() <= 50 && str.length() > 0) { int tg = count(str);  if (tg == -1) { continue;  } else {
                    System.out.println(tg);  }

            } else { continue;  }

        }
    } public static int count(String str) {
        Map<Character, Integer> map = new HashMap<Character, Integer>();  for (int i = 0; i < str.length(); i++) { char c = str.charAt(i);//toCharArray()也可以  if (c >= 'A' && c <= 'Z') { if (map.containsKey(c)) {//若统计过  map.put(c, map.get(c) + 1);  } else {
                    map.put(c, 1);  }
            } else { return -1;  }

        } if (map.size() == 2) {
            Set<Character> keys = map.keySet();  int a = 0;  int b = 0;  for (Character key : keys) {
                Integer index = map.get(key);  if (index == 1) {
                    a = index;  } else { if (a == 1) { return index + 1;  } else { return 2;  }
                }
            } return 2;  } else if (map.size() == 1) { return 1;  } else { return 0;  }
    }
}


 
编辑于 2017-08-16 19:26:47 回复(1)
package 彩色的砖块;
import java.util.*;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		while (in.hasNext()) {
			String str = in.nextLine();
			System.out.println(getCount1(str));
			System.out.println(getCount2(str));
		}
	}
	// 时间复杂度为O(n)的方法,就是判断字符串中字符种类的个数,只有一种字符就是1,两种字符2,多种字符为0
	private static int getCount1(String str) {
		char[] chas = str.toCharArray();
		int[] arr = new int[256];
		for (int i = 0; i < chas.length; i++) {
			arr[chas[i]]++;
		}
		int sum = arr[chas[0]];
		for (int i = 1; i < chas.length; i++) {
			if (chas[i] != chas[i - 1]) {
				sum += arr[chas[i]];
				break;
			}
		}
		if (sum == arr[chas[0]]) {
			return 1;
		}
		if (sum == str.length()) {
			return 2;
		}
		return 0;
	}
	// 时间复杂度为O(nlogn的方法),不过看了下时间,好像这种方法还要快1ms,空间也占用的少
	private static int getCount2(String str) {
		int count = 0;
		char[] chas = str.toCharArray();
		Arrays.sort(chas);
		for (int i = 1; i < chas.length; i++) {
			if (chas[i] != chas[i - 1]) {
				count++;
			}
		}
		if (count == 0) {
			return 1;
		}
		if (count == 1) {
			return 2;
		}
		return 0;
	}
}

发表于 2017-08-15 10:09:54 回复(2)
//用一个obj对象来存储出现过的不同字符,当出现的不同字符大于2,则输出0,等于2,则输出2,等于1,则输出1
var str=readline();
var strarr=str+"".split("");
var obj={};
var allow=true;
var count=0;
for(var i=0;i<strarr.length;i++){
    if(obj[strarr[i]]){
        obj[strarr[i]]++;
    }
    else{
        obj[strarr[i]]=1;
        count++;
    }
    if(count>2){
        allow=false;
        break;
    }
}

if(allow){
    if(count==1){
        print(1);
    }
    else{
        print(2);
    }
}
else{
    print(0);
}

发表于 2017-08-14 15:43:26 回复(4)
int main()
{
    string str;
    cin >> str;
    set<char> s;
    for (char c: str)
        s.insert(c);

    if (s.size() > 2)
        cout << 0 << endl;
    else if (s.size() == 2)
        cout << 2 << endl;
    else
        cout << 1 << endl;
    return 0;
}

发表于 2017-08-14 14:07:31 回复(2)
//AC代码:
#include<stdio.h>
#include<set>
using namespace std;
int main(){
    char a[1000];
    int i;
    while(scanf("%s",a)!=EOF){
        set<char> s;
        for(i=0;a[i]!='\0';i++) s.insert(a[i]);
        if(s.size()<=2) printf("%d\n",s.size());
        else printf("0\n");
    }
}

发表于 2017-08-23 17:49:44 回复(0)
超过2种颜色,一定不可能最多只有一对颜色相异的砖块相邻,不存在符合题意的排列;只有1种颜色,那排列只有一种;最复杂的就是2种颜色,一定可以化成题目中demo的形式,排列只有两种。
s = input()
color_nums = len(set(s))
if color_nums == 2:
    print(2)
elif color_nums < 2:
    print(1)
else:
    print(0)


发表于 2020-12-17 11:10:13 回复(0)
看到大佬们的简洁思路,再看看我自己”超时“代码,微微一笑绝对不哭🤣🤣🤣🤣🤣🤣😂😂
#include<bits/stdc++.h>
using namespace std;
int main(){
    string str;
    cin>>str;
    vector<char>v;
    int sum=0;
    for(auto s:str)v.push_back(s);
    sort(v.begin(),v.end());//在使用前需要对欲排列数组按升序排序,否则只能找出该序列之后的全排列数。
    do{
        int count=0;
        for(int i=0;i<v.size()-1;i++){
            if(v[i]!=v[i+1]){
                count++;
            }
        }
        if(count<=1)sum++;
    }while(next_permutation(v.begin(), v.end()));
    cout<<sum;
    return 0;
}


编辑于 2020-10-19 20:20:52 回复(0)
#include <bits/stdc++.h>
using namespace std;

int main(){
    string s;
    cin>>s;
    int cnt=0;
    bool M[26]={false};
    for(auto &c: s){
        if(!M[c-'A']){
            M[c-'A'] = true;
            cnt++;
        }
        if(cnt>2){
            cnt = 0;
            break;  
        }
    }
    printf("%d\n", cnt);
    return 0;
}

发表于 2020-09-04 01:55:12 回复(0)
  import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        char[] chs = sc.next().toCharArray();
        Arrays.sort(chs); //排序
        int count = 0;
        for(int i = 0;i<chs.length-1;++i){
            if(chs[i]!=chs[i+1]){
                ++count;
                if(count>1)
                    break;
            }
        }
        if(count == 0)
            System.out.println(1);
        else if(count==1)
            System.out.println(2);
        else
            System.out.println(0);
    }
}

发表于 2019-06-25 14:24:14 回复(0)
//如果超过三种颜色就不可能只有一对相邻的,直接输出0
//如果是两种颜色,就有两种可能,一种“A”在前面,一直“B”在前面,直接输出2
//如果只有一种颜色直接输出1
#include<iostream>
#include<string>
#include<set>
using namespace std;
#define MaxSize 51
int main(){
    string str;
    cin >> str;
    set<char> s;
    for (int i = 0; i < str.size(); i++){
        s.insert(str[i]);
    }
    int rec = s.size();
    if (rec >= 3)
        cout << 0 << endl;
    if (rec == 2)
        cout << 2 << endl;
    if (rec == 1)
        cout << 1 << endl;
    return 0;
}

发表于 2019-04-20 17:30:43 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main()
{
    char s[100]={0};
    scanf("%s",s);
    map<char,int>mp;
    for(int i=0;i<strlen(s)-1;i++) mp[s[i]]++;
    if(mp.size()<=2)cout<<mp.size();
    else{cout<<0;}
    return 0;
}

编辑于 2019-04-03 16:28:44 回复(0)
#include<iostream>
#include<vector>
#include<string>
using namespace std;

int main()
{
    string a;
    int temp[30]={0};
    cin>>a;
    for(int i = 0;i<a.length();i++)
        temp[a[i]-'A']++;
    int sum = 0;
    for(int i = 0;i<30;i++){
        if(temp[i]>0)
            sum++;
        if(sum>2){
            cout<<0<<endl;
            break;
        }
    }
    if(sum==2)
        cout<<2<<endl;
    else if(sum==1)
        cout<<1<<endl;
    return 0;
}

发表于 2019-03-27 17:51:51 回复(1)
n = list(input())
if len(set(n)) == 1:
    print(1)
elif len(set(n)) == 2:
    print(2)
else:
    print(0)

发表于 2019-03-21 17:14:41 回复(0)

热门推荐

通过挑战的用户

查看代码