测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串。注意这里的字符串包含空格,即空格也可能是要求被统计的字符之一。当读到'#'时输入结束,相应的结果不要输出。
对每个测试用例,统计第1行中字符串的每个字符在第2行字符串中出现的次数,按如下格式输出: c0 n0 c1 n1 c2 n2 ... 其中ci是第1行中第i个字符,ni是ci出现的次数。
I THIS IS A TEST i ng this is a long test string #
I 2 i 3 5 n 2 g 2
#include<stdio.h> #include<string.h> int main() { char str1[5], str2[80]; while (1) { int i = 0; int count[128] = { 0 }; while ((str1[i] = getchar()) != '\n' && str1[i] != '#' && (str1[i])!=EOF) i++; if (str1[i] == '#'|| str1[i]== EOF) break; str1[i] = '\0'; gets(str2); for (i = 0 ; i <strlen(str2); i++) count[str2[i]]++; for (i = 0 ; i < strlen(str1); i++) printf("%c %d\n", str1[i],count[str1[i]]); } return 0; }一直提示段错误,突然想到没有判断文件是否结束。
#include <iostream> using namespace std; int main() { string s1,s2; while(getline(cin,s1)){ if(s1=="#") break; getline(cin,s2); for(int i=0;i<s1.size();i++){ int num=0; for(int j=0;j<s2.size();j++){ if(s1[i]==s2[j]){ num++; } } cout<<s1[i]<<" "<<num<<endl; } } return 0; }
#include<iostream> #include<string> #include<cstring> using namespace std; int main(){ string str1,str2; while(getline(cin,str1)){ if(str1=="#") break; getline(cin,str2); char ch1[5],ch2[80]; strcpy(ch1,str1.c_str()); strcpy(ch2,str2.c_str()); int len1=strlen(ch1),len2=strlen(ch2); int *count=new int[len1]; for(int i=0;i<len1;i++) count[i]=0; for(int i=0;i<len2;i++){ for(int j=0;j<len1;j++){ if(ch2[i]==ch1[j]){ count[j]++; } } } for(int i=0;i<len2;i++){ cout<<ch1[i]<<' '<<count[i]<<endl; } } }
#include
#include
#include
using namespace std;
const int maxn = 1e5 + 5;
int hashT[maxn];
char s1[10];
char s2[85];
int main() {
int len1, len2;
while(gets(s1) && strcmp(s1, "#") != 0) {
memset(hashT, 0, sizeof(hashT));
gets(s2);
len1 = strlen(s1);
len2 = strlen(s2);
for(int i = 0; i < len2; i++) {
hashT[s2[i]]++;
}
for(int i = 0; i < len1; i++) {
printf("%c %d\n", s1[i], hashT[s1[i]]);
}
}
return 0;
}
#include <stdio.h> #include <string.h> #define N 81 int main() { char s1[N], s2[N]; int len1, len2; int count; while(gets(s1)) { if(s1[0]=='#') break; gets(s2); len1=strlen(s1); len2=strlen(s2); for(int i=0; i<len1; i++) { count=0; for(int j=0; j<len2; j++) { if(s1[i]==s2[j]) count++; } printf("%c %d\n", s1[i], count); } } return 0; }
//本代码思路:创建一个ascii整型数组,收集第二行字符串中每个字符出现的次数 //然后按第一行字符串的需要打印次数 import java.util.Scanner; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int[] ascii = new int[127]; char[] target = sc.nextLine().toCharArray(); char[] str = sc.nextLine().toCharArray(); for(int i=0;i<str.length;i++){ ascii[(int)(str[i])-1]++; } for(int i=0;i<target.length;i++){ char c = (char)(target[i]); System.out.println(c+" "+ascii[(int)(target[i])-1]); } } } }
#include <cstring> #include <cstdio> using namespace std; int main() { //freopen("date.txt", "r", stdin); char str1[6], str2[81]; while(gets(str1) && str1[0] != '#'){ gets(str2); int count[128] = {0};//统计128个ASCII出现的次数,每次统计都要清零 for(int i = 0; i < strlen(str2); i++)//统计str2中每个字符出现的次数,只需遍历一遍 count[(int) str2[i]]++; for(int i = 0; i < strlen(str1); i++) printf("%c %d\n", str1[i], count[(int) str1[i]]); } return 0; }
#include<cstdio> #include<cstring> #include<iostream> #include <string> using namespace std; int main() { while (true) { string str1; string str2; getline(cin, str1); if (str1[0] == '#') { break; } getline(cin, str2); char str3[100] = {0}; char str4[100] = {0}; strcpy(str3, str1.c_str()); strcpy(str4, str2.c_str()); int len = strlen(str3); for (int i = 0; i < len; i++) { int count = 0; int len1 = strlen(str4); for (int j = 0; j < len1; j++) { if (str1[i] == str2[j]) { count++; } } printf("%c %d\n", str1[i], count); } } return 0; 有没有大神告诉我我这个为什么AC不过呀
#include <stdio.h> #include <math.h> #include <stdlib.h> #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; const int MAXN = 5; struct Count{ char c; int n; }; //统计字符 int main(){ string str, str2; while (getline(cin, str)) { if (str == "#") { break; } int num; Count s[MAXN]; //将所有s.n 赋初值0 for (int i = 0; i < str.length(); i++){ s[i].c = str[i]; s[i].n = 0; } //统计要记录的字符个数 num = str.length(); //获取要统计的字符串 getline(cin, str2); for (int i = 0; i < str2.length(); i++){ for (int j = 0; j < num; j++){ if (s[j].c == str2[i]) { s[j].n++; } } } for (int i = 0; i < num; i++){ cout << s[i].c << " " << s[i].n << endl; } } return 0; }
#include <string> #include <iostream> using namespace std; int main() { char a[5]; char b[80]; while (cin.getline(a,5)&&a!="#") { cin.getline(b, 80); for (int i = 0; a[i] != '\0'; i++) { int count = 0; for (int j = 0; b[j] != '\0'; j++) { if (a[i] == b[j]) { count++; } } cout << a[i] << ' ' << count << endl; } } return 0; }
#include <iostream> #include <algorithm> #include <string> using namespace std; int main() { string str1; string str2; getline(cin, str1); getline(cin, str2); int sum = 0; for (int i = 0; i < str1.size(); i++) { cout << str1[i]<<" "; for (int j = 0; j < str2.size(); j++) { if (str1[i] == str2[j]) sum++; } cout << sum << endl; sum = 0; } return 0; }
#include<iostream> #include<string> #include<vector> #include<cmath> #include<algorithm> using namespace std; int main(){ string s1,s2; while(getline(cin,s1)&&s1!="#"){ //cin.get(); getline(cin,s2); for(int i=0;i<s1.length();i++){ int k=0; for(int j=0;j<s2.length();j++){ if(s2[j]==s1[i]) k++; } cout<<s1[i]<<' '<<k<<endl; } } }
def str_match(s, t): res = {} # for i in range(len(t)): # res[t[i]] = 0 c = 0 for i in t: c = 0 for j in s: if j == i: c += 1 print("{} {}".format(i, c)) while True: try: t = input() if t == '#': break s = input() str_match(s, t) except: break