《程序员》第9期智慧擂台题目——高频词汇提取

简介: 本文为原创,如需转载,请注明作者和出处,谢谢!     面对浩瀚的信息海洋,找到想要的资源有时真的是不容易。在大量文字中搜索高频词汇是信息搜索和数据压缩的共通课题。
本文为原创,如需转载,请注明作者和出处,谢谢!

    面对浩瀚的信息海洋,找到想要的资源有时真的是不容易。在大量文字中搜索高频词汇是信息搜索和数据压缩的共通课题。
这次智慧擂台请大家在一个比较庞大的英文文本中找出M个数量最多的短语(由N个单词组成)。统一处理相同的文本文件,该文本只包含英文单词、空格和回行符,比较谁的程序效率最高。

  程序输入:M,N,文本文件路径(M不超过20,N不超过8)
程序输出:高频短语及其数量清单

擂台规则:提交符合以上要求的可执行程序,语言招式不限,点到为止;
我们将在统一的环境下对每位选手的作品进行公平的测试,
比较出综合用时最少的程序。

源程序


import  java.io. * ;
import  java.util. * ;

class  tt
{
    
public  String phrase;
    
public   int  count;
}

public   class  searchphrase
{

    
private   static  LinkedHashMap phrase  =   new  LinkedHashMap();
    
static  tt[] max_phrase;

    
private   static  Vector SeparateString(String s)
    {
        Vector v 
=   new  Vector();
        String temp 
=   "" ;
        
for  ( int  i  =   0 ; i  <  s.length(); i ++ )
        {
            
if  (s.charAt(i)  !=   '   ' )
            {
                temp 
+=  s.charAt(i);
            }
            
else
            {
                
if  (temp  !=   "" )
                    v.add(temp);
                temp 
=   "" ;
            }
        }
        
if  (temp  !=   "" )
            v.add(temp);
        
return  v;
    }

    
private   static   void  swap( int  pos,  int  count, String phrase)
    {
        
int  i;
        
if  (max_phrase[pos  -   1 ].count  <  count)
        {
            
for  (i  =  pos  -   1 ; i  >   0 ; i -- )
            {
                
if  (max_phrase[i  -   1 ].count  >  max_phrase[i].count)
                    
break ;
            }
            max_phrase[pos].count 
=  max_phrase[i].count;
            max_phrase[pos].phrase 
=  max_phrase[i].phrase;
            max_phrase[i].count 
=  count;
            max_phrase[i].phrase 
=  phrase;
        }

    }

    
private   static   void  adjust_max( int  count, String phrase)
    {
        
int  i, j;
        
if  (count  <=  max_phrase[max_phrase.length  -   1 ].count)
            
return ;
        
for  (i  =  max_phrase.length  -   1 ; i  >=   0 ; i -- )
        {
            
if  (max_phrase[i].phrase.equals(phrase))
            {
                max_phrase[i].count 
=  count;
                
if  (i  >   0 )
                {
                    swap(i, count, phrase);
                }
                
return ;
            }
        }
        max_phrase[max_phrase.length 
-   1 ].count  =  count;
        max_phrase[max_phrase.length 
-   1 ].phrase  =  phrase;
        
if  (i  >   0 )
        {
            swap(max_phrase.length 
-   1 , count, phrase);
        }
    }

    
private   static   void  js(Vector v,  int  n)
    {
        String s;
        
for  ( int  i  =   0 ; i  <  v.size()  -  n  +   1 ; i ++ )
        {
            s 
=   "" ;
            
for  ( int  j  =  i; j  <  i  +  n; j ++ )
            {
                s 
+=  v.get(j)  +   "   " ;
            }
            
int  count  =   1 ;
            
if  (phrase.containsKey(s.hashCode()))
            {
                count 
=  Integer.parseInt(phrase.get(s.hashCode()).toString());
                count
++ ;
            }
            phrase.put(s.hashCode(), count);
            adjust_max(count, s);
        }
    }

    
public   static   void  main(String[] args)
    {
        
try
        {
            
long  t;
            
int  m, n;
            String path;
            m 
=  Integer.parseInt(args[ 0 ]);
            n 
=  Integer.parseInt(args[ 1 ]);
            path 
=  args[ 2 ];
            max_phrase 
=   new  tt[m];
            
for  ( int  i  =   0 ; i  <  m; i ++ )
            {
                max_phrase[i] 
=   new  tt();
                max_phrase[i].count 
=   0 ;
                max_phrase[i].phrase 
=   "" ;
            }
            t 
=  ( new  java.util.Date()).getTime();
            java.io.FileReader fr 
=   new  java.io.FileReader(path);
            java.io.BufferedReader br 
=   new  BufferedReader(fr);
            String s;

            Vector v 
=   null ;

            
while  ((s  =  br.readLine())  !=   null )
            {
                v 
=  SeparateString(s);
                js(v, n);
            }
            
for  ( int  i  =   0 ; i  <  m; i ++ )
            {
                System.out.println(max_phrase[i].phrase);
                System.out.println(max_phrase[i].count);
                System.out.println();
            }
            t 
=  ( new  java.util.Date()).getTime()  -  t;
            System.out.print(t);
            System.out.println(
"  ms " );
        }
        
catch  (Exception e)
        {
            System.out.println(e.getMessage());
        }

    }

}

测试结果1:m = 20 n = 8 

under games played won drawn lost goals for
71

tabulated under games played won drawn lost goals
70

games played won drawn lost goals for against
70

May Xinhua Following are the results from the
69

played won drawn lost goals for against and
59

won drawn lost goals for against and points
59

Jan Xinhua Following are the results from the
48

Chinas economic efficiency indicators of the sector of
39

The industrial statistics include all stateowned enterprises and
39

industrial statistics include all stateowned enterprises and the
39

statistics include all stateowned enterprises and the nonstateowned
39

include all stateowned enterprises and the nonstateowned ones
39

all stateowned enterprises and the nonstateowned ones with
39

stateowned enterprises and the nonstateowned ones with annual
39

enterprises and the nonstateowned ones with annual sales
39

and the nonstateowned ones with annual sales income
39

Xinhua Chinas economic efficiency indicators of the sector
39

the nonstateowned ones with annual sales income over
39

nonstateowned ones with annual sales income over million
39

up percent over the same period last year
35

13594 ms


测试结果2  m = 10 n = 5

Xinhua Following are the results
295

May Xinhua Following are the
209

Following are the results from
183

are the results from the
176

April Xinhua Following are the
141

Jan Xinhua Following are the
122

billion yuan billion US dollars
120

won drawn lost goals for
88

played won drawn lost goals
88

Dec Xinhua Following are the
87

12437 ms

 
以上源程序是采用的是最简单的方法,谁有更好,效率更高的方法,请跟贴!!

目录
相关文章
|
消息中间件 Unix Linux
Linux进程间通信(IPC)介绍:详细解析IPC的执行流程、状态和通信机制
Linux进程间通信(IPC)介绍:详细解析IPC的执行流程、状态和通信机制
1209 1
|
2月前
|
人工智能 决策智能
让 AI 自己教自己:多智能体自监督打开了递归自我改进的口子
伯克利与Sakana提出MASS:让基础模型自主组建多智能体团队,通过“进化工作流+自生成轨迹微调”两轮循环实现递归自提升。两轮后性能提升1.2–1.6倍,同等token下效果超越单智能体训练——自监督信号质量更高。(239字)
210 0
|
前端开发
用 CSS 实现兼容性渐变背景效果
【10月更文挑战第17天】
846 151
|
数据采集 测试技术 数据安全/隐私保护
Playwright测试中避免使用no-wait-for-timeout的原因
在Web应用自动化测试中,Playwright作为首选框架,其稳定性至关重要。不当使用`no-wait-for-timeout`会导致测试结果不稳定、不符合真实用户体验且难以调试。推荐采用显式等待策略和合理设置超时时间,结合代理IP技术提高测试成功率和数据多样性。示例代码展示了如何在Playwright中配置代理IP进行数据抓取及分类统计。遵循这些最佳实践可确保测试既可靠又贴近实际用户场景。
1103 4
Playwright测试中避免使用no-wait-for-timeout的原因
|
人工智能 架构师 程序员
通义灵码上线一周年:超 600 万下载量,国内用户规模第一,新功能有奖测评
通义灵码一周年,新功能有奖测评火热开启!参与活动就有机会获得机械键盘、华为手环等好礼哦,快来了解吧。
|
人工智能 定位技术
挑战杯丨2025年度中国青年科技创新“揭榜挂帅”擂台赛阿里云榜题发布!用AI助力乡村振兴丨云工开物
第十九届“挑战杯”竞赛2025年度中国青年科技创新“揭榜挂帅”擂台赛,由阿里巴巴公益、阿里云等主办。赛事以AI技术助力乡村振兴为主题,鼓励高校师生设计长虹乡特色文创产品、农特产品包装等。作品需紧扣开化特色。评选标准涵盖创意、文化呈现和技术应用等方面。比赛设擂主奖及多项奖项。报名截止至2025年6月30日,作品提交截止至8月15日。
|
运维 安全 网络安全
常用的运维工具:SSH和远程连接工具详解
常用的运维工具:SSH和远程连接工具详解
1448 3
|
缓存 算法 NoSQL
短信验证码登录接口,如何防止恶意攻击
该文讨论了移动应用中常见的手机短信验证码登录方式,后端实现通常涉及两个API:获取短信验证码和短信验证码登录。在设计时,为增强短信验证码接口的安全性,提出了几种无需使用Redis等存储介质的方案:1) 使用数字签名,基于时间戳或随机数生成唯一签名进行验证;2) 基于时间的有效期验证,通过加密或修改时间戳形式确保安全性;3) 应用TOTP算法,按时间生成动态码进行比对;4) 利用JWTToken生成带有限期的签名进行验证。这些方法旨在防止恶意攻击并优化登录接口性能。
1502 1
|
人工智能 分布式计算 数据可视化
大模型私有化部署全攻略:硬件需求、数据隐私、可解释性与维护成本挑战及解决方案详解,附示例代码助你轻松实现企业内部AI应用
【10月更文挑战第23天】随着人工智能技术的发展,大模型在各领域的应用日益广泛。然而,将其私有化部署到企业内部面临诸多挑战,如硬件资源需求高、数据隐私保护、模型可解释性差、更新维护成本高等。本文探讨了这些挑战,并提出了优化硬件配置、数据加密、可视化工具、自动化更新机制等解决方案,帮助企业顺利实现大模型的私有化部署。
1702 1
|
机器学习/深度学习 Kubernetes 算法框架/工具
容器服务 ACK 大模型推理最佳实践系列一:TensorRT-LLM
在 ACK 中使用 KServe 部署 Triton+TensorRT-LLM