牛牛有N个字符串,他想将这些字符串分类,他认为两个字符串A和B属于同一类需要满足以下条件:
A中交换任意位置的两个字符,最终可以得到B,交换的次数不限。比如:abc与bca就是同一类字符串。
现在牛牛想知道这N个字符串可以分成几类。
首先输入一个正整数N(1 <= N <= 50),接下来输入N个字符串,每个字符串长度不超过50。
输出一个整数表示分类的个数。
4 abcd abdc dabc bacd
1
public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); HashSet<String> set = new HashSet<String>(); while(n!=0) { char[] array = scanner.next().toCharArray(); Arrays.sort(array); set.add(String.valueOf(array)); n--; } scanner.close(); System.out.println(set.size()); } }
#include <string> #include <iostream> #include <map> #include <vector> #include <algorithm> using namespace std; int main() { int n; scanf("%d", &n); map<string, size_t> Maps; for (size_t i = 0; i < n; i++) { string current; cin >> current; sort(current.begin(), current.end()); Maps[current]++; } cout << Maps.size() << endl; return 0; }利用map中唯一键值,来统计排序后的字符串个数。
str_list=[] N=int(input()) for i in range(N): str_list.append(''.join(sorted(input()))) print(len(set(str_list)))
package com.heima.schedule.test; import org.checkerframework.checker.units.qual.min; import java.util.Scanner; public class Test02 { public static void main(String[] args) { Scanner in = new Scanner(System.in); int num = in.nextInt(); char[][] arr = new char[num][]; for (int i = 0; i < num; i++) { arr[i] = in.next().toCharArray(); sort(arr[i]); } char[][] s = new char[num][]; s[0] = arr[0]; int resultLen = 1; for (int i = 1; i < num; i++) { boolean flag = false; for (int j = 0; j < resultLen; j++){ // 比较 if (ifSame(s[j], arr[i])){ flag = true; break; } } if (! flag) { s[resultLen++] = arr[i]; } } System.out.println(resultLen); } /** * 判断两个字符数组是否相同 * @param c1 * @param c2 * @return */ private static boolean ifSame(char[] c1, char[] c2) { if (c1.length != c2.length) { return false; } for (int i = 0; i < c1.length; i++) { if (c1[i] != c2[i]) { return false; } } return true; } /** * 字符排序 * @param chars */ private static void sort(char[] chars){ for (int i = 0; i < chars.length - 1; i++) { int minIndex = i; for (int j = i + 1; j < chars.length; j++) { if (chars[j] < chars[minIndex]) { minIndex = j; } } if (minIndex != i) { char temp = chars[i]; chars[i] = chars[minIndex]; chars[minIndex] = temp; } } } }
#include<iostream> (720)#include<cstring> #include<cmath> (808)#include<algorithm> #include<iomanip> using namespace std; int main() { int a[55][27] = { 0 }; for (int i = 0; i < 55; i++) for (int j = 0; j < 27; j++) a[i][j] = 0; int count = 0; int n; cin >> n; string str[55]; for (int i = 0; i < n; i++) { cin >> str[i]; } for (int i = 0; i < n; i++) { int len = str[i].length(); for (int j = 0; j < len; j++) { a[i][str[i][j] - 97]++; } } for (int i = 0; i < n; i++) { if (a[i][0] == 50 + i) continue; count++; for (int j = i + 1; j < n; j++) { int flag = 1; for (int k = 0; k < 26; k++) { if (a[i][k] != a[j][k]) flag = 0; } if (flag == 1) { a[j][0] = 50 + j; } } } cout << count; }
const main = (strings) => { var types = []; for(let i = 0; i < strings.length; i++){ //let str = strings[i].split('').sort().join(''); let str = strings[i].split('').sort((item1, item2)=>{ return item1.localeCompare(item2, 'zh-CN'); }).join(''); //适配中文 if(!types.includes(str))types.push(str) } return types.length } console.log(main(['东京', '京东']))// expect 1
import java.util.*;
public class StringClassify {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
String[] str = new String[N];
HashSet set = new HashSet();
for (int i = 0; i < N; i++) {
str[i] = sortStr(sc.next());
set.add(str[i]);
}
System.out.println(set.size());
}
public static String sortStr(String str) {
char[] chars = str.toCharArray();
Arrays.sort(chars);
return new String(chars);
}
}
// // main.cpp // violent_enumeration // // Created by LU QI on 2017/11/06. // Copyright © 2017 LU QI. All rights reserved. // #include <iostream> #include <vector> #include <string> using namespace std; bool compared(string a, string b){ if(a.length()!=b.length()) return false; else{ int count=0; bool h=true; __SIZE_TYPE__ n = a.size(); for(int i=0;i<n;i++){ h = true; for(int j=0;j<n&&h;j++){ if(b[j]==a[i]){ count++; h = false; b[j]='#'; } } } if(count == n) return true; else return false; } } int main(int argc, const char * argv[]) { int N; cin>>N; string array[N]; int count=N; for(int i=0;i<N;i++){ cin>>array[i]; } for(int i=0;i<N-1;i++){ if(array[i]!="#"){ for(int j= i+1; j<N; j++){ if(array[j]!="#"){ if(compared(array[i], array[j])){ count--; array[j]="#"; } } } } } cout<<count<<endl; return 0; }
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring> //牛客网环境下string操作函数需要引用这个库
using namespace std;
void sortStr(string &str) {
for (int i = 0; i < (int)str.size() - 1; i++) {
for (int j = (int)str.size() - 1; j > 0; j--) {
if (str[j - 1] > str[j]) {
char temp = str[j];
str[j] = str[j - 1];
str[j - 1] = temp;
}
}
}
}
int main(int argc, const char * argv[]) {
int n;
cin>>n;
int count = n;
string str[50];
for (int i = 0; i < n; i++) {
cin>>str[i];
sortStr(str[i]);
}
sort(str, str + n);
int i = 0;
while (i < n - 1) {
int j;
for (j = n - 1; j > i; j--) {
char a[50], b[50];
strcpy(a, str[i].c_str());
strcpy(b, str[j].c_str());
if (!strcmp(a, b)){
count -= j - i;
i = j + 1;
break;
}
}
if (i != j + 1) {
i++;
}
}
cout<<count;
return 0;
}
#include<iostream> #include <string.h> using namespace std; int* chartoint(char *ch) { int size=strlen(ch); int* ret = new int[size]; for(int i=0;i<size;i++) { ret[i] = ch[i]; } return ret; } char *sort(char*ch) { for(int i=0;i<strlen(ch)-1;i++) for(int j=0;j<strlen(ch)-1-i;j++) { if(chartoint(ch)[j]>chartoint(ch)[j+1]) { char ch1 = ch[j]; ch[j] = ch[j+1]; ch[j+1] = ch1; } } return ch; } bool boolequal(char*ch1,char*ch2) { if(strlen(ch1)!=strlen(ch2)) return 0; for(int i=0;i<strlen(ch1);i++) { if(ch1[i]!=ch2[i]) return 0; } return 1; } int main() { int n,ret=1; cin >> n; char ch[n][50]; char newch[50][50]; for(int i=0;i<n;i++) cin >> ch[i]; for(int i=1;i<n;i++) { for(int j=i-1;j>=0;j--) { if(boolequal(sort(ch[j]),sort(ch[i]))) break; if(j==0) ret ++; } } cout<<ret; }
import java.util.*; public class Main{ public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); TreeSet <String> tree=new TreeSet<String>(); while(sc.hasNext()){ int num=sc.nextInt(); int i=0; while(i<num){ char ch[]=sc.next().toCharArray(); Arrays.sort(ch); tree.add(String.valueOf(ch)); i++; } System.out.println(tree.size()); } sc.close(); } }
#include <iostream> #include <vector> #include <string> #include <algorithm> int main() { using namespace std; int n; while (cin >> n) { vector<string> str(n); for (int i = 0; i < n; i++) cin >> str[i]; int ans = 0; for (int i = 0; i < n; i++) { int index[127] = { 0 }; string temp_str = ""; if (str[i] != "") { temp_str = str[i]; for (int j = 0; j < str[i].size(); j++) { index[str[i][j]] ++; } ans++; } else { continue; } for (int j = i; j < n; j++) { if (str[j] != "" && str[j].size() == temp_str.size()) { int temp[127] = { 0 }; for (int k = 0; k < str[j].size(); k++) { temp[str[j][k]] ++; } int k = 0; for (k = 0; k < 127; k++) { if (index[k] != temp[k]) break; } if (k == 127) { str[j] = ""; } } } } cout << ans << endl; } return 0; }