在一行上输入一个长度不超过
、由大小写字母混合构成的字符串
,代表初始字符串。
在一行上输出一个整数,代表字符串
的最小循环节的长度。
abcabcD
4
在字符串
中,最小循环节为
,其长度为
。
脑筋急转弯题,实际上就是求有多少个不同的字母。
import java.util.Scanner;
import java.util.HashSet;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
HashSet<Character> set = new HashSet<>();
for (char c : str.toCharArray()){
set.add(c);
}
System.out.println(set.size());
}
}
} #include <bits/stdc++.h>
using namespace std;
int main() {
string str;
cin >> str;
set<int> A;
int a[52];
memset(a,0,sizeof a);
for(int i = 0 ; i < str.size() ; i ++){
if(str[i] >= 'a' && str[i] <= 'z'){
a[str[i] - 'a']++;
}
else if(str[i] >= 'A' && str[i] <= 'Z'){
a[str[i]-'A' + 26]++;
}
}
int sum = 0;
for(int i = 0 ; i < 52 ; i++){
if(a[i] > 0){sum++;}
}
cout << sum << endl;
}
set函数做法 #include <bits/stdc++.h>
using namespace std;
int main() {
set<char> a;
string str;
cin >> str;
for(int i = 0 ; i < str.size() ; i ++){
a.insert(str[i]);
}
cout << a.size() << endl;
return 0;
}
#include<iostream>
#include<set>
using namespace std;
int main(){
string s;
cin>>s;
set<char> st;
for(auto&ch:s) st.insert(ch);
cout<<st.size();
}