牛牛变得黑化了,想要摧毁掉地球。但他忘记了开启地球毁灭器的密码。牛牛手里有一个字符串S,牛牛还记得从S中去掉一个字符就恰好是正确的密码,请你帮牛牛求出他最多需要尝试多少次密码。
如样例所示S = "ABA",3个可能的密码是"BA", "AA", "AB".
当S = "A", 牛牛唯一可以尝试的密码是一个空的密码,所以输出1.
输入包括一个字符串S,字符串长度length(1 ≤ length ≤ 50),其中都是从'A'到'Z'的大写字母。
输出一个整数,表示牛牛最多需要尝试的密码次数。
ABA
3
//十分简短的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; }
// 思路一:一个个去字符,存于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); } }
蠢方法,去重 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()); } }
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(); } }
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;}}
不知道到底哪里错了。求大神指点下。 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); } } } 您的代码已保存答案错误:您提交的程序没有通过所有的测试用例
密码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);}
<?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));
}
?>
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); } } }
#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; }
使用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; }
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());}}