一个单词出现的频率%
第一行一个正整数 n,代表这篇文章的单词总数。接下来 n 行每行一个字符串,代表一个单词,单词仅由大小写英文字母组成。1<= n <= 106保证所有的字符串长度之和不超过 106
仅一行一个整数表示答案。
5 I I am a boy
4
单词 I 出现的频率为 40% ,其他单词出现的频率均为 20%。所以都可以作为关键词。
class num_nn(): def __init__(self): self.num_n = [] self.count_m = 0 def play(self): self.n = input("请输入单词数: ") for i in range(0,int(self.n)): num = input("请输入单词: ") self.num_n.append(num) self.num_n_set = set(self.num_n) for item in self.num_n_set: print("the %s has found %s" %(item,self.num_n.count(item))) if int(self.num_n.count(item))/int(self.n) >= 0.01: self.count_m = self.count_m + 1 print(self.count_m) if __name__ == '__main__': numnn = num_nn() numnn.play()
import java.util.HashMap; import java.util.Scanner; public class Main { public static void main(String args[]) { HashMap<String,Integer> hm=new HashMap<String,Integer>(); Scanner sc=new Scanner(System.in); int n=sc.nextInt(); sc.nextLine(); int res=0; for(int i=0;i<n;i++){ String tmp=sc.nextLine(); if(hm.get(tmp)==null){ hm.put(tmp,1); }else{ hm.put(tmp,hm.get(tmp)+1); } } for(String key:hm.keySet()){ if((hm.get(key)*100)>=n){ res++; } } System.out.println(res); } }
#JavaScript var arr=new Array(); //构造数组 while(line=readline()){ //将第一行的总字数,和后面每一行的 arr.push(line); //单词作为元素加入数组 } var nuum= arr[0]; //初始化总词数 var key=0; //初始化关键词数 var times=nuum*0.01; //计算作为关键词的最低频率 for(let u = 1;u<=nuum;u++){//循环每个单词 var word = 0; //初始化重复单词数 if(arr.indexOf(arr[u])==u){//如果该单词重复出现则跳过该单词 for(let j=1;j<=nuum;j++){//循环遍历数组 if(arr[u]==arr[j]){ word++; //计算该词重复单词数 if(word>=times){//判断该单词是否为关键词 key++; break; //代码优化,判断为关键词时立刻跳出本次循环 } } } } } print(key)
python处理数据真的很容易
n=eval(input()) word_list = [] word_count =[] for i in range(n): word=input() if word in word_list: word_count[word_list.index(word)]+=1 else: word_list.append(word) word_count.append(1) word_frequency=list(map(lambda x:(1 if x/n>=0.01 else 0),word_count)) print(sum(word_frequency))
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string, int> m;
int ans = 0, n, total;
cin>>n;
total = n;
while(n--)
{
string s;
cin>>s;
m[s]++;
}
for (auto i = m.begin(); i != m.end(); i++)
{
double tempInt = i->second;
double percent = tempInt/total;
if (percent - 0.01 >= 0)
ans++;
}
cout<<ans<<endl;
return 0;
}
// 情况1:使用整数相除 if(n / ((*iter).second) <= 100){ m++; } // 情况2:使用小数相除 if((float)((*iter).second) / (float)n >= 0.01){ m++; }
const count = parseInt(readline()) const dic = {} for(let i =0;i<count;i++){ let temp = readline() if(dic[temp]) dic[temp]++ else dic[temp] = 1 } let answer = 0 Object.keys(dic).forEach(item=>{ if (Math.floor(dic[item]/count*100)) answer++ }) console.log(answer)利用了字典的特性
n=int(input().strip()) A=[] for i in range(n): A.append(input().strip()) uA=[] for i in range(len(A)): if A[i] not in uA: uA.append(A[i]) c=0 for i in range(len(uA)): if A.count(uA[i])/len(A)>=0.01: c+=1 print(c)
using System; internal class Program { private static void Main(string[] args) { int number = int.Parse(Console.ReadLine()); int targetALL = 0; string[] eng = new string[number]; for (int i = 0; i < number; i++) { String curr = Console.ReadLine(); eng[i] = curr; } string[] checkEng = new string[number];//检测过频率的放入 for (int i = 0, j = 0; i < number; i++) { bool isRepeat = ISRepeat(eng[i], checkEng);//检测当前对象是否已经判断过 if (!isRepeat) { float num = Check(eng[i], eng); if (num >= 0.1) { targetALL++; } checkEng[j] = eng[i]; j++;//判断过频率的放入checkEng中,避免重复判断 } else { continue; } } Console.WriteLine(targetALL); } private static float Check(string target, string[] str) {//判断指定字符在字符串**现的概率 int number = 0; for (int i = 0; i <= str.Length - 1; i++) { if (str[i] == target) { number++;// } } float allNumber = str.Length; float retu = (float)((number) / (allNumber));//todo 最好同类型相加减乘除,否则可能发生隐式转换 丢失精度 return retu;// } private static bool ISRepeat(string target, string[] str) {//判断指定字符在字符串**是否重复 for (int i = 0; i <= str.Length - 1; i++) { if (str[i] == target) { return true; } } return false; } } 思路如图:两个函数一个判断是否重复,避免重复判断,一个进行频率检测,如果大于0.1频率,则最终数字++, 问题是在数据小没问题,数据量大(1000)出现输出0错误,求解 case通过60%,出现输出0错误
n=int(input('请输入该文章所含单词数:')) qty=0 #关键词个数 t=() #这里一定要用不可变对象,列表是可变的,如果用列表的话,lst2改变,t这里的元素也会变 for i in range(n): a=input('请输入文中的单词:') t+=(a,) #元组的增加 lst2=[] for y in t: lst2.append(y) #将元组中的元素赋给列表 print(t,id(t),type(t)) print(lst2,id(lst2),type(lst2)) for s1 in lst2: #遍历列表中的元素 m=lst2.count(s1) if m > 1: lst2.remove(s1) #删除相同的元素,只留一个,最终lst2中只含有不同的元素 continue else: continue print('删减后的lst2:',lst2) for j in lst2: #遍历lst2,即所有不同的单词全部计算一遍 h = t.count(j) #得出该单词在含相同元素的元组中,所占的个数 print(j,'出现的次数为',h,end='') x=h/n print('其占比为',x) if x>=0.01: qty+=1 continue else: continue print('总共有{0}个单词可作为关键词'.format(qty))
import java.io.*; import java.util.HashMap; public class Main{ public static void main(String[] args) throws IOException{ BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(reader.readLine()); HashMap<String,Integer> map = new HashMap<>(); String str; for(int i = 0;i < n;i++){ str = reader.readLine(); map.put(str,map.getOrDefault(str,0) + 1); } double a = 0.01*n; int res = 0; for(int num : map.values()){ if(num >= a) res++; } System.out.println(res); } }