第一行是一个整数T(1 ≤ T ≤ 100)表示测试样例数;接下来T行,每行给定一个分身后的电话号码的分身(长度在3到10000之间)。
输出T行,分别对应输入中每行字符串对应的分身前的最小电话号码(允许前导0)。
4 EIGHT ZEROTWOONE OHWETENRTEO OHEWTIEGTHENRTEO
0 234 345 0345
要先依次解码具有独一无二字符的数字,具有这样特点的数字有五个分别是FOUR(U),SIX(X), TWO(W),EIGHT(G),ZERO(Z),可以根据独特字符的个数直接判断有多少个相应的数字,例如有 3个U那么就一定有3个FOUR...,解码完成这五个数字之后另外的数字也会由于这些数字的移除 而具有了独一无二的字符,这样的数字有FIVE(F),THREE(T),FIVE找到之后,只有SEVEN含 有V,所以又可以依据V字符的个数解码SEVEN的个数,最后剩下的ONE和NINE也具有了自己的 标志性字符分别是ONE(O),NINE(I),需要注意的是原始数字和最终出现的数字还有一个转换 的过程(加8取个位数),所以还要相应转换回去。最后要注意的是,要求每行字符串对应的 分身前的最小电话号码,不要傻傻的按照字典序排序,这样时间复杂度过高,对于这个问题 可以用桶排序,10个bucket分别用于统计0-9出现的次数,最终桶中存储的结果依次输出就是 所有组合中最小的数字。 #include <iostream> #include <string.h> #include <vector> #include <algorithm> using namespace std; int main(){ int i = 0, j = 0; int n; while (cin >> n){ string s; for (i = 0; i < n; i++){ cin >> s; vector<int> iimap(256); for (j = 0; j < s.size(); j++){ iimap[s[j]]++; } vector<int> res(10); int count = iimap['U']; res[6] = count; iimap['F'] -= count; iimap['O'] -= count; iimap['U'] -= count; iimap['R'] -= count; count = iimap['X']; res[8] = count; iimap['S'] -= count; iimap['I'] -= count; iimap['X'] -= count; count = iimap['W']; res[4] = count; iimap['T'] -= count; iimap['W'] -= count; iimap['O'] -= count; count = iimap['G']; res[0] = count; iimap['E'] -= count; iimap['I'] -= count; iimap['G'] -= count; iimap['H'] -= count; iimap['T'] -= count; count = iimap['Z']; res[2] = count; iimap['Z'] -= count; iimap['E'] -= count; iimap['R'] -= count; iimap['O'] -= count; count = iimap['F']; res[7] = count; iimap['F'] -= count; iimap['I'] -= count; iimap['V'] -= count; iimap['E'] -= count; count = iimap['V']; res[9] = count; iimap['S'] -= count; iimap['E'] -= count; iimap['V'] -= count; iimap['E'] -= count; iimap['N'] -= count; count = iimap['T']; res[5] = count; iimap['T'] -= count; iimap['H'] -= count; iimap['R'] -= count; iimap['E'] -= count; iimap['E'] -= count; count = iimap['O']; res[3] = count; iimap['O'] -= count; iimap['N'] -= count; iimap['E'] -= count; count = iimap['I']; res[1] = count; iimap['N'] -= count; iimap['I'] -= count; iimap['N'] -= count; iimap['E'] -= count; for (int k = 0; k < res.size(); k++){ for (int l = 0; l < res[k]; l++) cout << k; } cout << endl; } } return 0; }
**,**题 import java.util.Scanner; /** * Created by Evergreen on 2017/8/10. */ public class Main { public static void handle(String str){ str=str.toLowerCase(); int[] hash=new int[10]; StringBuffer sb=new StringBuffer(str); while(sb.toString().contains("z")){//zero //2 hash[2]++; sb.deleteCharAt(sb.indexOf("z")); sb.deleteCharAt(sb.indexOf("e")); sb.deleteCharAt(sb.indexOf("r")); sb.deleteCharAt(sb.indexOf("o")); } while(sb.toString().contains("x")){//six hash[8]++; sb.deleteCharAt(sb.indexOf("s")); sb.deleteCharAt(sb.indexOf("i")); sb.deleteCharAt(sb.indexOf("x")); } while(sb.toString().contains("s")){//seven hash[9]++; sb.deleteCharAt(sb.indexOf("s")); sb.deleteCharAt(sb.indexOf("e")); sb.deleteCharAt(sb.indexOf("v")); sb.deleteCharAt(sb.indexOf("e")); sb.deleteCharAt(sb.indexOf("n")); } while(sb.toString().contains("u")){//four hash[6]++; sb.deleteCharAt(sb.indexOf("f")); sb.deleteCharAt(sb.indexOf("o")); sb.deleteCharAt(sb.indexOf("u")); sb.deleteCharAt(sb.indexOf("r")); } while(sb.toString().contains("f")){//five hash[7]++; sb.deleteCharAt(sb.indexOf("f")); sb.deleteCharAt(sb.indexOf("i")); sb.deleteCharAt(sb.indexOf("v")); sb.deleteCharAt(sb.indexOf("e")); } while(sb.toString().contains("g")){//eight hash[0]++; sb.deleteCharAt(sb.indexOf("e")); sb.deleteCharAt(sb.indexOf("i")); sb.deleteCharAt(sb.indexOf("g")); sb.deleteCharAt(sb.indexOf("h")); sb.deleteCharAt(sb.indexOf("t")); } while(sb.toString().contains("w")){//two hash[4]++; sb.deleteCharAt(sb.indexOf("t")); sb.deleteCharAt(sb.indexOf("w")); sb.deleteCharAt(sb.indexOf("o")); } while(sb.toString().contains("h")&&!sb.toString().contains("g")){//three hash[5]++; sb.deleteCharAt(sb.indexOf("t")); sb.deleteCharAt(sb.indexOf("h")); sb.deleteCharAt(sb.indexOf("r")); sb.deleteCharAt(sb.indexOf("e")); sb.deleteCharAt(sb.indexOf("e")); } while(sb.toString().contains("o")&&!sb.toString().contains("z")){//one hash[3]++; sb.deleteCharAt(sb.indexOf("o")); sb.deleteCharAt(sb.indexOf("n")); sb.deleteCharAt(sb.indexOf("e")); } while(sb.toString().contains("n")) {//nine hash[1]++; sb.deleteCharAt(sb.indexOf("n")); sb.deleteCharAt(sb.indexOf("i")); sb.deleteCharAt(sb.indexOf("n")); sb.deleteCharAt(sb.indexOf("e")); } for(int i=0;i<10;i++){ for(int j=1;j<=hash[i];j++){ System.out.print(i); } } System.out.println(); } public static void main(String[] args) { Scanner sc=new Scanner(System.in); while(sc.hasNextInt()){ int n=sc.nextInt(); for(int i=0;i<n;i++) handle(sc.next()); } } }
思路基本是一楼的思路 但是他的代码太长了
#include<iostream> #include<string> #include<cstring> #include<vector> #include<algorithm> using namespace std; string a[10] = {"ZERO", "TWO", "FOUR", "SIX", "EIGHT", "ONE", "THREE", "FIVE", "SEVEN", "NINE"}; int num_map[] = {0, 2, 4, 6, 8, 1, 3, 5, 7, 9}; char dic[] = {'Z', 'W', 'U', 'X', 'G', 'O', 'T', 'F', 'S', 'I'}; int ha[26]; vector<int> ans; int main(){ int T; scanf("%d", &T); while(T--){ string s; cin>>s; ans.clear(); memset(ha, 0, sizeof(ha)); for(int i = 0; i < s.length(); i++) ha[s[i]-'A']++; for(int i = 0; i < 10; i++){ int times = ha[dic[i]-'A']; if(times > 0){ for(int j = 0; j < a[i].size(); j++) ha[a[i][j]-'A'] -= times; for(int k = times; k > 0; k--) ans.push_back((num_map[i]+2) % 10); } } sort(ans.begin(), ans.end()); for(int i=0;i<ans.size();i++) printf("%d", ans[i]); printf("\n"); } }
将以上数字统计后排序可得。
import java.util.*; import java.lang.*; public class Main { static String[] table = { "ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE" }; static int retL = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i, j; int n = sc.nextInt(); sc.nextLine(); String str; int[] mark = new int[26]; int[] ret = new int[10000]; int temp = 0; while (sc.hasNext()) { retL = 0; str = sc.nextLine(); for(i=0;i<str.length();i++) { mark[str.charAt(i)-'A']++; } if(mark['Z' - 'A'] != 0) handle(mark, ret, 2, 'Z'); if(mark['W' - 'A'] != 0) handle(mark, ret, 4, 'W'); if(mark['U' - 'A'] != 0) handle(mark, ret, 6, 'U'); if(mark['X' - 'A'] != 0) handle(mark, ret, 8, 'X'); if(mark['G' - 'A'] != 0) handle(mark, ret, 0, 'G'); if(mark['F' - 'A'] != 0) handle(mark, ret, 7, 'F'); if(mark['O' - 'A'] != 0) handle(mark, ret, 3, 'O'); if(mark['R' - 'A'] != 0) handle(mark, ret, 5, 'R'); if(mark['S' - 'A'] != 0) handle(mark, ret, 9, 'S'); if(mark['I' - 'A'] != 0) handle(mark, ret, 1, 'I'); Arrays.sort(ret,0,retL); for (i = 0; i < retL; i++) { System.out.print(ret[i]); } System.out.println(); } } public static void handle(int[] mark, int[] ret, int num, char point) { int temp = mark[point - 'A'],i; for (i = 0; i < temp; i++) { ret[retL++] = num; } for (i = 0; i < table[(num+8)%10].length(); i++) { mark[table[(num+8)%10].charAt(i) - 'A'] -= temp; } } }
import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner sc=new Scanner(System.in); int num=Integer.parseInt(sc.nextLine());//行数 String[] sArr=new String[num];//构建一个String数组存放输入的每行字符串 for(int i=0;i<num;i++) { sArr[i]=sc.nextLine(); } for(String each:sArr) { //构建一个map用于存储代表性字母的个数 //0-->Z; 2-->W; 4-->U; 6-->X; 8-->G;(唯一对应,每个字母的个数就代表其数字的个数) //1-->O(1的个数计算方法:num(O)-num(0)-num(2)-num(4)= map(O)-map(Z)-map(W)-map(U)) //3-->R(3的个数计算方法:num(R)-num(0)-num(4)= map(R)-map(Z)-map(U)) //5-->F(5的个数计算方法:num(F)-num(4)= map(F)-map(U)) //7-->S(7的个数计算方法:num(S)-num(6)= map(S)-map(X)) //9-->N(注意,9里面有两个N,故计算结果应除以2:(num(N)-num(1)-num(7))/2=(map(N)-count(1)-count(7))/2) Map<Character,Integer> map=new HashMap<Character,Integer>() { { put('Z',0); put('W',0); put('U',0); put('X',0); put('G',0); put('O',0); put('R',0); put('F',0); put('S',0); put('N',0); } }; for(int i=0;i<each.length();i++) {//统计当前字符串中有代表性字母的个数 char c=each.charAt(i); if(map.containsKey(c)) map.put(c, 1+map.get(c)); else continue;//遇到不在字典里的字母就跳过 } int[] count=new int[10]; count[0]=map.get('Z'); count[2]=map.get('W'); count[4]=map.get('U'); count[6]=map.get('X'); count[8]=map.get('G'); count[1]=map.get('O')-map.get('Z')-map.get('W')-map.get('U'); count[3]=map.get('R')-map.get('Z')-map.get('U'); count[5]=map.get('F')-map.get('U'); count[7]=map.get('S')-map.get('X'); count[9]=(map.get('N')-count[1]-count[7])/2;//NINE中有两个N int[] countnew=new int[10]; for(int i=0;i<10;i++){ countnew[i]=count[(i+8)%10];//使用countnew数组存储加8以前的个数 } for(int i=0;i<10;i++) {//按照从0到9的顺序输出 for(int j=0;j<countnew[i];j++) {//每个数字输出的次数 System.out.print(i); } } System.out.println();//输出完一个字符串换行 } } }
#include<stdio.h> #include<string.h> int v[10]={2,4,6,8,0,3,5,7,9,1},n,i,j,hash[10],tmp[26],cnt[26]; char dst[10][10]={"ZERO","TWO","FOUR","SIX", "EIGHT","ONE","THREE","FIVE","SEVEN","NINE"},s[10005]; void dfs(int *x){ int i,cnt=0,j; for(i=0;i<26;i++) if(x[i]==0) cnt++; if(cnt==26){ for(i=0;i<10;i++) for(j=0;j<hash[i];j++) printf("%d",i); printf("\n"); return; } for(i=0;i<26;i++) tmp[i]=x[i]; for(i=0;i<10;i++){ int flag=1; for(j=0;j<dst[i][j]!='\0';j++){ x[dst[i][j]-'A']--; if(x[dst[i][j]-'A']<0){ flag=0;break; } } if(flag==1){ hash[v[i]]++,dfs(x);break; } for(j=0;j<26;j++) x[j]=tmp[j]; } } int main(){ for(scanf("%d",&n);n--;dfs(cnt),memset(hash,0,sizeof(hash))){ scanf("%s",s); memset(cnt,0,sizeof(cnt)); for(j=0;s[j]!='\0';j++) cnt[s[j]-'A']++; } }//dfs删除,注意删除顺序,0 2 4 6 8是具备唯一特征字母的 //排除0 2 4 6 8,之后,1 3 5 7是具备唯一特征的,最后就剩9了
var n=readline(); while(str=readline()){ changedNum(str); } function changedNum(str){ var reg1=[/Z/g,/W/g,/U/g,/X/g,/G/g];//0,2,4,6,8; var reg2=[/O/g,/H/g,/F/g,/S/g,/N/g];//1,3,5,7,9; var result=[]; var num_arr=new Array(10).fill(0); for(let i=0,j=0;i<reg1.length;i++){ var num=str.match(reg1[i]); if(num){ num_arr[i+j]=num.length; } j++; } for(let i=0,j=1;i<reg2.length;i++){ var num=str.match(reg2[i]); switch(i){ case 0:{//1="O"-0-2-4 if(num){ num_arr[i+j]=num.length-num_arr[0]-num_arr[2]-num_arr[4]; } break; } case 1:{//3="H"-8 if(num){ num_arr[i+j]=num.length-num_arr[8]; } break; } case 2:{//5="F"-4 if(num){ num_arr[i+j]=num.length-num_arr[4]; } break; } case 3:{//7="S"-6 if(num){ num_arr[i+j]=num.length-num_arr[6]; } break; } case 4:{//9=("N"-1-7)/2 if(num){ num_arr[i+j]=(num.length-num_arr[1]-num_arr[7])/2; } break; } default: break; } j++; } for(let i=0;i<num_arr.length;i++){ for(let j=0;j<num_arr[i];j++){ if(i>=8){ result.push(i-8); }else{ result.push(i+2); } } } console.log(result.sort((x,y)=>{return x-y}).join("")); }
#include<iostream> #include<vector> #include<string> #include<algorithm> using namespace std; int main() { char ch[] = { 'Z', 'W', 'X', 'S', 'U', 'O', 'R', 'F', 'G', 'I' }; int dig[] = { 0, 2, 6, 7, 4, 1, 3, 5, 8, 9 }; string ch_all[] = { "ZERO", "TWO", "SIX", "SEVEN", "FOUR", "ONE", "THREE", "FIVE", "EIGHT", "NINE" }; int T; for (auto &i : dig) { i-= 8; if (i < 0) i+= 10; } while (cin >> T) { while (T--) { int count[26] = { 0 }; string str; cin >> str; vector<int> dig_cout; for (auto i : str) { count[i - 'A']++; } for (int i = 0; i < 10; i++) { int tmp_c = count[ch[i] - 'A']; if (tmp_c>0) for (auto j : ch_all[i]) count[j - 'A'] -= tmp_c; while (tmp_c--) dig_cout.push_back(dig[i]); } sort(dig_cout.begin(), dig_cout.end()); for (auto i : dig_cout) cout << i; cout << endl; } } return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n;cin>>n; while(n--) { string s; cin>>s; vector<int> v; int a[10]={0}; while(s.find('Z')!=s.npos) { s.erase(s.find('Z'),1); s.erase(s.find('E'),1); s.erase(s.find('R'),1); s.erase(s.find('O'),1); a[2]++; } while(s.find('G')!=s.npos) { s.erase(s.find('G'),1); s.erase(s.find('E'),1); s.erase(s.find('I'),1); s.erase(s.find('H'),1); s.erase(s.find('T'),1);a[0]++; } while(s.find('X')!=s.npos) { s.erase(s.find('X'),1); s.erase(s.find('I'),1); s.erase(s.find('S'),1); a[8]++; } while(s.find('S')!=s.npos) { s.erase(s.find('V'),1); s.erase(s.find('E'),1); s.erase(s.find('E'),1); s.erase(s.find('S'),1); s.erase(s.find('N'),1);a[9]++; } while(s.find('W')!=s.npos) { s.erase(s.find('W'),1); s.erase(s.find('T'),1); s.erase(s.find('O'),1);a[4]++; } while(s.find('U')!=s.npos) { s.erase(s.find('F'),1); s.erase(s.find('U'),1); s.erase(s.find('O'),1); s.erase(s.find('R'),1);a[6]++; } while(s.find('F')!=s.npos) { s.erase(s.find('F'),1); s.erase(s.find('I'),1); s.erase(s.find('V'),1); s.erase(s.find('E'),1);a[7]++; } while(s.find('T')!=s.npos) { s.erase(s.find('T'),1); s.erase(s.find('H'),1); s.erase(s.find('R'),1); s.erase(s.find('E'),1); s.erase(s.find('E'),1);a[5]++; } while(s.find('I')!=s.npos) { s.erase(s.find('N'),1); s.erase(s.find('I'),1); s.erase(s.find('N'),1); s.erase(s.find('E'),1);a[1]++; } a[3]=s.length()/3; for(int i=0;i<10;i++) v.insert(v.begin(),a[i],i ); sort(v.begin(), v.end()); for(int i=0;i<v.size();i++) cout<<v[i]; cout<<endl; } }很LOW 的题,只要复制粘贴的快,再长也是一分钟的事。
def func(x): #计算乱序字母中的ZERO,ONE,TWO...NINE的数量 dict = [ 0 for i in range(10)] tab = [ 0 for i in range(10)] num_Z = x.count('Z') #0 ZERO num_W = x.count('W') #2 TWO num_U = x.count('U') #4 FOUR num_X = x.count('X') #6 SIX num_G = x.count('G') #8 EIGHT num_S = x.count('S') - num_X # 7 SEVEN num_F = x.count('F') - num_U # 5 FIVE num_T = x.count('T') - num_W - num_G # 3 THREE num_O = x.count('O') - num_Z - num_W - num_U # 1 ONE num_I = x.count('I') - num_F - num_X - num_G # 9 NINE dict[0] = num_Z dict[1] = num_O dict[2] = num_W dict[3] = num_T dict[4] = num_U dict[5] = num_F dict[6] = num_X dict[7] = num_S dict[8] = num_G dict[9] = num_I tab[0] = dict[8] tab[1] = dict[9] tab[2] = dict[0] tab[3] = dict[1] tab[4] = dict[2] tab[5] = dict[3] tab[6] = dict[4] tab[7] = dict[5] tab[8] = dict[6] tab[9] = dict[7] s = "" for i in range(len(tab)): s += str(i)*tab[i] return s import sys x = sys.stdin.readlines() for i in range(1,len(x)): print(func(x[i]))
//number代表字符串,bucket是转化后的数组 #include <iostream> #include <string> #include <map> using namespace std; int main(){ int n; cin>>n; while(n--){ string s; cin >> s; map <char,int> number; int* bucket = new int [10]; for(int i = 0; i < s.length(); i++) number[s[i]]++; bucket[2] = number['Z']; bucket[4] = number['W']; bucket[6] = number['U']; bucket[8] = number['X']; bucket[0] = number['G']; bucket[3] = number['O'] - bucket[2] - bucket[4] - bucket[6]; bucket[5] = number['R'] - bucket[2] - bucket[6]; bucket[7] = number['F'] - bucket[6]; bucket[9] = number['S'] - bucket[8]; bucket[1] = number['I'] - bucket[7] - bucket[8] - bucket[0]; for(int i = 0;i<10;i++){ while(bucket[i]--) cout<<i; } cout<<endl; } return 0; }
//丧心病狂的题目 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int t = Integer.parseInt(in.nextLine()); for (int i = 0; i < t; i++) { doWork(in); } } public static void doWork(Scanner in) { String s = in.nextLine(); int[] cs = new int[27]; for (int i = 0; i < s.length(); i++) { cs[s.charAt(i) - 'A']++; } int[] num = new int[10]; num[0] = cs['Z' - 'A']; num[6] = cs['X' - 'A']; num[2] = cs['W' - 'A']; num[4] = cs['U' - 'A']; num[8] = cs['G' - 'A']; num[3] = cs['T' - 'A'] - num[2] - num[8]; num[7] = cs['S' - 'A'] - num[6]; num[5] = cs['V' - 'A'] - num[7]; num[1] = cs['O' - 'A'] - num[0] - num[2] - num[4]; num[9] = (cs['N' - 'A'] - num[7] - num[1]) / 2; StringBuilder builder = new StringBuilder(); for (int i = 8; i <=9; i++) { for (int j = 0; j < num[i]; j++) { builder.append((i+2)%10); } } for (int i = 0; i <=7; i++) { for (int j = 0; j < num[i]; j++) { builder.append(i+2); } } System.out.println(builder.toString()); } }
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { char ch[] = {'Z','W','X','S','U','O','R','F','G','I'}; int d[] = {0,2,6,7,4,1,3,5,8,9}; string num[] = {"ZERO","TWO","SIX","SEVEN","FOUR","ONE","THREE","FIVE","EIGHT","NINE"}; int T; for(int i=0;i<10;i++) { d[i] -= 8; if(d[i]<0) d[i] += 10; } while(cin>>T) { while(T--) { int count[26]={0}; string s; cin>>s; vector<int> result; for(int i=0;i<s.length();i++) count[s[i]-'A']++; for(int i=0;i<10;i++) { int t = count[ch[i]-'A']; if(t>0) for(int j=0;j<num[i].length();j++) count[num[i][j]-'A'] -= t; while(t--) result.push_back(d[i]); } sort(result.begin(), result.end()); for(int i=0;i<result.size();i++) cout<<result[i]; cout<<endl; } } return 0; }
#include
#include
#include
#include
#include
using namespace std;
typedef struct
{
int num;
char index;
string numword;
int numcnt;
}node;
void handle(string &numstr)
{
vector nodevec(10);
nodevec[0].num = 2;
nodevec[0].index = 'Z';
nodevec[0].numword = "ZERO";
nodevec[1].num = 4;
nodevec[1].index = 'W';
nodevec[1].numword = "TWO";
nodevec[2].num = 6;
nodevec[2].index = 'U';
nodevec[2].numword = "FOUR";
nodevec[3].num = 8;
nodevec[3].index = 'X';
nodevec[3].numword = "SIX";
nodevec[4].num = 0;
nodevec[4].index = 'G';
nodevec[4].numword = "EIGHT";
nodevec[5].num = 3;
nodevec[5].index = 'O';
nodevec[5].numword = "ONE";
nodevec[6].num = 5;
nodevec[6].index = 'T';
nodevec[6].numword = "THREE";
nodevec[7].num = 7;
nodevec[7].index = 'F';
nodevec[7].numword = "FIVE";
nodevec[8].num = 9;
nodevec[8].index = 'S';
nodevec[8].numword = "SEVEN";
nodevec[9].num = 1;
nodevec[9].index = 'E';
nodevec[9].numword = "NINE";
vector res;
for(vector::iterator iter = nodevec.begin();iter != nodevec.end();++iter)
{
node n = *iter;
n.numcnt = count_if(numstr.begin(), numstr.end(), bind2nd(equal_to(), n.index));
for(int k = 0;k < n.numcnt;k++)
{
for(string::iterator iter2 = n.numword.begin();iter2 != n.numword.end();++iter2)
{
numstr.erase(find_if(numstr.begin(), numstr.end(), bind2nd(equal_to(), *iter2)));
}
res.push_back(n.num);
}
}
sort(res.begin(), res.end(), less());
copy(res.begin(), res.end(), ostream_iterator(cout));
cout << endl;
}
int main()
{
int cnt;
while(cin >> cnt)
{
vector strvec(cnt);
for(int i = 0;i < cnt;i++)
{
cin >> strvec[i];
}
for(int i = 0;i < cnt;i++)
{
string numstr = strvec[i];
handle(numstr);
}
}
return 0;
}
时间:2018年8月14日
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
vector<int> func(string str) {
map<int, int> resmap;
map<char, int> amap;
for(int i = 0;i < str.size();i++) {
amap[str[i]]++;
}
//2
resmap[2] += amap['Z'];
for(int i = 0;i < resmap[2];i++) {
amap['Z']--;
amap['E']--;
amap['R']--;
amap['O']--;
}
resmap[4] += amap['W'];
for(int i = 0;i < resmap[4];i++) {
amap['T']--;
amap['W']--;
amap['O']--;
}
resmap[6] += amap['U'];
for(int i = 0;i < resmap[6];i++) {
amap['F']--;
amap['O']--;
amap['U']--;
amap['R']--;
}
resmap[8] += amap['X'];
for(int i = 0;i < resmap[8];i++) {
amap['S']--;
amap['I']--;
amap['X']--;
}
resmap[0] += amap['G'];
for(int i = 0;i < resmap[0];i++) {
amap['E']--;
amap['I']--;
amap['G']--;
amap['H']--;
amap['T']--;
}
resmap[3] += amap['O'];
for(int i = 0;i < resmap[3];i++) {
amap['O']--;
amap['N']--;
amap['E']--;
}
resmap[5] += amap['R'];
for(int i = 0;i < resmap[5];i++) {
amap['T']--;
amap['H']--;
amap['R']--;
amap['E']--;
amap['E']--;
}
resmap[7] += amap['F'];
for(int i = 0;i < resmap[7];i++) {
amap['F']--;
amap['I']--;
amap['V']--;
amap['E']--;
}
resmap[9] += amap['V'];
for(int i = 0;i < resmap[9];i++) {
amap['S']--;
amap['E']--;
amap['V']--;
amap['E']--;
amap['N']--;
}
resmap[1] += amap['I'];
for(int i = 0;i < resmap[1];i++) {
amap['N']--;
amap['I']--;
amap['N']--;
amap['E']--;
}
vector<int> vec;
for(auto i : resmap) {
for(int j = 0;j < i.second;j++){
vec.push_back(i.first);
}
}
return vec;
}
int main() {
int n;
cin >> n;
for(int i = 0;i < n;i++) {
string str;
cin >> str;
vector<int> vec = func(str);
for(auto num : vec) {
cout << num;
}
cout << endl;
}
return 0;
}
//找到数字和单词的对应关系,单词和字母的对应关系 import java.util.HashMap; import java.util.Scanner; /** * * @author JSZha0 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); String[] sb = new String[n]; in.nextLine(); while(in.hasNext()) System.out.println(minPhoneNumber(in.nextLine())); } } public static String minPhoneNumber(String str) { //hashmap存储字符串中字母个数 HashMap<Character, Integer> map = new HashMap<>(); for(char i=65; i<91; i++) { map.put(i, 0); } //统计字母个数 for(char s : str.toCharArray()) { map.put(s, map.get(s)+1); } //数组对应关系,index表示数字,存储的值表示数字的个数 int[] ch = new int[10]; // ch[2] = map.get('Z'); // ZERO 0 2 ch[4] = map.get('W'); // TWO 2 4 ch[6] = map.get('U'); // FOUR 4 6 ch[8] = map.get('X'); // SIX 6 8 ch[0] = map.get('G'); // EIGHT 8 0 ch[5] = map.get('H')-ch[0]; // THREE-GIGHT 3 5 ch[7] = map.get('F')-ch[6]; //... ch[9] = map.get('V')-ch[7]; ch[3] = map.get('O')-ch[2]-ch[4]-ch[6]; ch[1] = map.get('I')-ch[0]-ch[8]-ch[7]; String s = ""; for(int i=0; i<10;i++) { while(ch[i]>0) { s += i; ch[i]--; } } return s; } }