【PTA】6-8 分词并显示

题样

#include<string.h>
#include<ctype.h>
void Input ( char *str )
{
	int i=0;
	char t=0;
	t=getchar();
	while(i<80&&t!='\n')
	{
		if(isalpha(t)||t==' ')
		{
			i++;
			*str++=t;
		}
		t=getchar();
	}
    *str='\0';
}
int Split_Count ( char *str,char **pStr )
{
	char *src,*drc;
	int i=0,flag=0;
	int count=0;
	int len=strlen(str);
	for(i=0;i<len;i++)
	{
		if(isalpha(*(str+i))&&flag==0)
		{
			flag=1;
			count++;
			*pStr++=str+i;

		}
		else 
		{
			if(*(str+i)==' '&&flag==1)
			{
				*(str+i)='\0';
				flag=0;
			}
		}
	}
	return count;
}
当心指针数组没有额外空间可以指,所以只能指向str的数组

`(*>﹏<*)′`(*>﹏<*)′`(*>﹏<*)′

 

Java PTA迷你搜索引擎是一个基于Java编程语言的简单搜索引擎项目。这个项目通常用于教学目的,帮助学生理解搜索引擎的基本原理和实现方法。以下是Java PTA迷你搜索引擎的一些关键组成部分和功能: ### 1. 数据抓取 搜索引擎的第一步是抓取网页内容。可以使用Java的URL类和HttpURLConnection类来实现网页的抓取。抓取到的内容可以存储在本地文件中以便后续处理。 ### 2. 文本处理 抓取到的网页内容需要进行文本处理,包括去除HTML标签、分词、去除停用词等。Java中有许多库可以帮助实现这些功能,例如Jsoup用于解析HTML,Lucene用于分词和索引。 ### 3. 索引构建 为了提高搜索效率,需要对处理后的文本进行索引。Lucene是一个强大的全文搜索库,可以用来构建倒排索引。倒排索引是一种数据结构,可以快速地查找包含特定关键词的文档。 ### 4. 搜索功能 搜索功能是搜索引擎的核心。用户在搜索框中输入关键词,搜索引擎会在索引中查找匹配的文档,按照一定的排序算法(如TF-IDF、PageRank等)返回结果。 ### 5. 用户界面 一个简单的用户界面可以提高用户体验。可以使用Java的Swing或JavaFX库来创建图形用户界面(GUI),或者使用HTML/CSS/JavaScript来创建Web界面。 ### 6. 性能优化 为了提高搜索引擎的性能,可以进行一些优化,例如缓存搜索结果、使用多线程进行行处理、压缩索引文件等。 ### 示例代码 以下是一个简单的示例代码,展示如何使用Java和Lucene构建一个基本的搜索引擎: ```java import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.document.StringField; import org.apache.lucene.document.TextField; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryparser.classic.QueryParser; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; public class MiniSearchEngine { public static void main(String[] args) throws Exception { // 创建分析器和索引目录 StandardAnalyzer analyzer = new StandardAnalyzer(); Directory index = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(analyzer); // 创建索引写入器 IndexWriter w = new IndexWriter(index, config); // 添加文档到索引 addDoc(w, "Lucene in Action", "193398817"); addDoc(w, "Lucene for Dummies", "55320055Z"); addDoc(w, "Managing Gigabytes", "55063554A"); addDoc(w, "The Art of Computer Science", "9900333X"); w.close(); // 搜索 String querystr = args.length > 0 ? args[0] : "lucene"; Query q = new QueryParser("title", analyzer).parse(querystr); // 执行搜索 IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(index)); ScoreDoc[] hits = searcher.search(q, 10).scoreDocs; // 显示搜索结果 System.out.println("Found " + hits.length + " hits."); for (int i = 0; i < hits.length; ++i) { int docId = hits[i].doc; Document d = searcher.doc(docId); System.out.println((i + 1) + ". " + d.get("isbn") + " " + d.get("title")); } } private static void addDoc(IndexWriter w, String title, String isbn) throws Exception { Document doc = new Document(); doc.add(new TextField("title", title, Field.Store.YES)); doc.add(new StringField("isbn", isbn, Field.Store.YES)); w.addDocument(doc); } } ``` ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值