首页 > 试题广场 >

黑化的牛牛

[编程题]黑化的牛牛
  • 热度指数:2305 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
牛牛变得黑化了,想要摧毁掉地球。但他忘记了开启地球毁灭器的密码。牛牛手里有一个字符串S,牛牛还记得从S中去掉一个字符就恰好是正确的密码,请你帮牛牛求出他最多需要尝试多少次密码。
如样例所示S = "ABA",3个可能的密码是"BA", "AA", "AB".
当S = "A", 牛牛唯一可以尝试的密码是一个空的密码,所以输出1.

输入描述:
输入包括一个字符串S,字符串长度length(1 ≤ length ≤ 50),其中都是从'A'到'Z'的大写字母。


输出描述:
输出一个整数,表示牛牛最多需要尝试的密码次数。
示例1

输入

ABA

输出

3
#include <string>
#include <iostream>
//十分简短的c++解法
#include <string>
#include <iostream>
 
using namespace std;
 
int main(){
    string s;
    cin>>s;
    //如果没有相邻的重复字母,则次数即为字符串的长度
    int count=s.size();
    for(int i=1;i<s.size();++i){
        //每有一对相邻的重复字母,就会有一次重复,因此次数减一
        if(s[i]==s[i-1]) --count;
    }
    cout<<count;
}

发表于 2017-06-19 13:06:04 回复(4)
// 思路一:一个个去字符,存于set中
import java.util.HashSet;
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		String str = in.next();
		
		HashSet<String> set = new HashSet<String>();
		for(int i=0;i<str.length();i++){
			StringBuilder sb = new StringBuilder(str); 
			sb.deleteCharAt(i);
			set.add(sb.toString());
		}
		System.out.println(set.size());
	}
}

//思路2:对于相邻的两个字符,如果相同的话,去除一个,得到的结果是相同的
//       问题转换为计算字符串当前字符是否和前面字符相同.
import java.util.Scanner;

public class Main {
	public static void main(String[] args){
		Scanner in = new Scanner(System.in);
		String str = in.next();
		int ans = 1;
		for(int i=1;i<str.length();i++){
			if(str.charAt(i)!=str.charAt(i-1)){
				ans++;
			}
		}
		System.out.print(ans);
	}
}
	

编辑于 2017-06-17 15:53:12 回复(2)
蠢方法,去重

import java.util.*;

public class Main{
    
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);
        String line = scanner.next();
        StringBuilder sb = new StringBuilder(line);
        HashSet<String>set = new HashSet<String>();
        for(int i=0, len = line.length();i<len;i++){
            sb.deleteCharAt(i);
            set.add(sb.toString());
            sb.insert(i,line.charAt(i));
        }
        System.out.println(set.size());
    }
}

发表于 2017-06-17 09:44:46 回复(0)
#include<string>
#include<iostream>
#include<set>
using namespace std;
set<string> s;
int main(){
    string x,tmp;
    cin>>x,tmp=x;
    for(int i=0;i<x.length();i++)
        x.erase(x.begin()+i),s.insert(x),x=tmp;
    cout<<s.size()<<endl;
}

发表于 2017-11-10 21:52:07 回复(0)
#include<iostream>
#include<string>
using namespace std;
intfp(intn, string s)
{
    intk=0;
    string kk[50];
    string bbb;
    if(n==1)
        return1;
    inti=0;
    while(i<n)
    {
            bbb.append(s,0,i);
            bbb.append(s,i+1,n);//将去掉第i个字符后的字符串存入bbb;
            for(intj=0;j<50;j++)
            {
                if(kk[j]==bbb)//去重复的字符串,例如AAA,只有一种可能
                    {
                        i++;
                        bbb.clear();
                    break;
                }
                elseif(j==k&&kk[k]!=bbb)
                {
                    kk[k++]=bbb;
                    i++;
            bbb.clear();
                    break;
                }
            }
         
    }
    returnk;
}
intmain()
{
    string ss;
    cin>>ss;
    intn=ss.length();
    intkk=fp(n,ss);
    cout<<kk<<endl;
    system("pause");
    return0;
}
发表于 2017-08-30 12:55:38 回复(0)
import java.util.HashSet;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        String string=sc.next();
        StringBuilder sb=new StringBuilder(string);
        
        HashSet hs=new HashSet();
        for(int i=0;i<string.length();i++){
            sb.deleteCharAt(i);
            hs.add(sb.toString());
            sb.insert(i,string.charAt(i));
        }
        System.out.println(hs.size());
    }
}
发表于 2018-03-23 11:52:33 回复(0)
//很多时候,运用STL函数相当方便
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
    string s_in;
    while(cin>>s_in)
    {  
        s_in.erase( unique( s_in.begin(), s_in.end() ), s_in.end() );
        int count = s_in.size();
        cout<<count<<endl;  
    }
    return 0;
}

编辑于 2017-08-27 19:04:57 回复(2)
import java.util.*;
public class quanguo42 {
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		while(sc.hasNext()){
			String s=sc.nextLine();
			StringBuffer sb=new StringBuffer(s);
			TreeSet<String> tree=new TreeSet<String>();
			for(int i=0;i<s.length();i++){
				sb=sb.deleteCharAt(i);
				tree.add(sb.toString());
				sb=new StringBuffer(s);
			}
			System.out.println(tree.size());
		}
		sc.close();
	}

}

编辑于 2017-08-14 19:27:35 回复(0)
importjava.util.Scanner;
 
publicclassMain {
    publicstaticvoidmain(String[] args) {
        Scanner in = newScanner(System.in);
        String s = in.nextLine();
        intn = StringsTrim(s);
        System.out.println(n);
    }
     
    publicstaticintStringsTrim(String s){
        intcount=s.length();
        charch[]=s.toCharArray();
        for(inti=0;i<ch.length-1;i++){
            if(ch[i+1]==ch[i]) {
                count--;
            }
        }
        returncount;
    }
}

发表于 2017-08-06 20:55:12 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String args[]) {
        Scanner scanner = new Scanner(System.in);
        String str = scanner.next();
        int rep = 0;
        for(int i = 0; i < str.length() - 1; i++) {
            if(str.charAt(i) == str.charAt(i + 1)) {
                rep++;
            }
        }
        int count = str.length() - rep;
        System.out.println(count);
    }
}
发表于 2017-07-30 15:09:58 回复(0)
不知道到底哪里错了。求大神指点下。
import java.util.Scanner;
public class Main{
    public static void main(String[] agrs){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.next();
            int num = 0;
            String[] str1 = str.split("");
            for(int i = 0; i<str.length()-1;i++){
                    if(str1[i].equals(str1[i+1])){
                        num++;
                    }
                }
           System.out.println(str.length()-num);
        }
    }
}


您的代码已保存
答案错误:您提交的程序没有通过所有的测试用例
case通过率为70.00%

测试用例:
AABACCCCABAA

对应输出应该为:

7

你的输出为:

8

发表于 2017-07-25 22:27:37 回复(0)
密码
while(str = readline()){
    var count=0;
    if(str.length == 1){
        count++;
    }else{
        str = str.split("");
        var arr=[];
        var res=[];
        var tem=[];
        for(var i=0; i<str.length; i++){
            tem = str.slice(0);
            tem.splice(i,1)
            arr.push(tem.join(""));
        }        
        for(var i = 0; i < arr.length; i++) {
            if(res.indexOf(arr[i]) === -1){
                res.push(arr[i]);
            }
        }         
        count = res.length;
    }  
    print(count);
}
编辑于 2017-07-14 10:27:14 回复(0)
#include<iostream>
#include<string>
using namespace std;
 
intmain(){
    intres=1;
    string str;
    getline(cin,str);
    if(str.length()==1){  cout<<1<<endl; return0; }
    inti=1;
    while(str[i]!='\0'){
        while(i<str.length() && str[i]==str[i-1]) i++;
        if(i!=str.length()) res++;
        i++;
    }
    cout<<res<<endl;
    return0;
}
发表于 2017-07-02 22:05:47 回复(0)
<?php
header("charset=utf-8");

$s = trim(fgets(STDIN));

$len = strlen($s);

if("^[A-Z]{1-50}+$"){

$arr = str_split($s);

$i = 0;

$j = 0;

while($i+1<$len){

if($arr[$i]==$arr[++$i]){

$j++;

}else{

continue;

}

}

$time = $len-$j;

echo $time;

return$time;

}else{

$s = trim(fgets(STDIN));

}

?>
编辑于 2017-06-22 16:57:39 回复(0)
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String line = sc.nextLine();
		if (line.length() < 0) {
			return;
		} else if (line.length() == 1) {
			System.out.println(1);
		} else {
			char[] ss = line.toCharArray();
			int re = 0;
			for (int i = 1; i < ss.length; i++) {
                                //后一个字符和前一个比较,相等则重复数加1
				if(ss[i]==ss[i-1]){
					re++;
				}
			}
                        //字符串长度减去重复数
			System.out.println(ss.length-re);
		}
	}
}

发表于 2017-06-19 16:44:20 回复(0)
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <set>

int main() {
    using namespace std;
    string str;
    while (cin >> str) {
        int len = str.size();
        set<string> strset;
        for (int i = 0; i < len; i++) {
            string temp = "";
            for (int j = 0; j < len; j++) {
                if (j != i) {
                    temp += str[j];
                }
            }
            strset.insert(temp);
        }
        cout << strset.size() << endl;
    }
    return 0;
}

发表于 2017-06-19 16:20:47 回复(0)
使用map
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <algorithm>


using namespace std;
int main()
{
	string s;
	cin >> s;
	map<string, int> mymap;
	int res = 0;
	if (s.size() == 1)
	{
		res = 1;
	}
	else
	{
		for (int i = 0; i < s.size(); ++i)
		{
			string ts = s;
			ts.erase(i, 1);
			mymap.insert(make_pair(ts, i));
		}

		for (auto i = mymap.begin(); i != mymap.end(); ++i)
		{
			++res;
		}
	}
	cout <<res;

	cin.get();
	cin.get();
    return 0;
}

发表于 2017-06-17 21:11:10 回复(0)
importjava.util.HashSet;
importjava.util.Scanner;
 
 
publicclassMain {
    publicstaticvoidmain(String[] args){
        Scanner sc = newScanner(System.in);
        String s = sc.next();
        char[] str = s.toCharArray();
        HashSet<String> hashset = newHashSet<String>();
        for(inti = 0; i < str.length; i++) {
            StringBuffer sb = newStringBuffer();
            for(intj=0;j<str.length;j++){
                if(j==i){
                    continue;
                }
                sb.append(str[j]);
            }
            hashset.add(sb.toString());
        }
        System.out.println(hashset.size());
         
    }
     
}

发表于 2017-06-17 19:59:33 回复(0)

热门推荐

通过挑战的用户

黑化的牛牛