OJ-1017中文分词模拟器

示例0

输入:

ilovechina

i,ilove,lo,love,ch,china,lovechina

输出:

ilove,china

示例1

输入:

ilovechina

i,love,china,ch,na,ve,lo,this,is,the,word

输出:

i,love,china

说明:

示例2

输入:

iat

i,love,china,ch,na,ve,lo,this,is,the,word,beauti,tiful,ful

输出:i,a,t

说明:单个字母,不在词库中且不成词则直接输出单个字母

示例3

输入:

ilovechina,thewordisbeautiful

i,love,china,ch,na,ve,lo,this,is,the,word,beauti,tiful,ful

输出:

i,love,china,the,word,is,beauti,ful

说明:标点符号为英文标点符号

复制代码
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;


public class 中文模拟分词器2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String input = in.nextLine();
        List<String> dict = Arrays.asList(in.nextLine().split(","));
        
        int len = input.length();
        StringBuilder sb = new StringBuilder();
        int i = 0;
        while (i < len) {
            int j = len;
            boolean found = false;
            while (j > i) {
                String s = input.substring(i, j);
                if (s.matches("[a-zA-Z]+") && (dict.contains(s) || s.length() == 1)) {
                    sb.append(s).append(",");
                    found = true;
                    i = j;
                    break;
                }
                j--;
            }
            if (!found) {
                i++;
            }
        }
        System.out.println(sb.substring(0, sb.length() - 1));
    }
}
相关推荐
百锦再6 分钟前
第12章 测试编写
android·java·开发语言·python·rust·go·erlang
无敌最俊朗@7 分钟前
C++ 并发与同步速查笔记(整理版)
开发语言·c++·算法
王哈哈^_^8 分钟前
【完整源码+数据集】课堂行为数据集,yolo课堂行为检测数据集 2090 张,学生课堂行为识别数据集,目标检测课堂行为识别系统实战教程
人工智能·算法·yolo·目标检测·计算机视觉·视觉检测·毕业设计
C2H5OH66613 分钟前
Netty详解-02
java·websocket·网络协议·tcp/ip·tomcat·netty·nio
夏鹏今天学习了吗23 分钟前
【LeetCode热题100(66/100)】寻找两个正序数组的中位数
算法·leetcode·职场和发展
墨染点香37 分钟前
LeetCode 刷题【151. 反转字符串中的单词】
算法·leetcode·职场和发展
跟着珅聪学java1 小时前
HttpServletRequest中的 Attribute(属性)生命周期和作用域是 Java Web 开发中的重要概念
java
ytttr8731 小时前
Landweber迭代算法用于一维、二维图像重建
人工智能·算法·机器学习
feifeigo1231 小时前
Matlab编写压缩感知重建算法集
人工智能·算法·matlab
m0_495562781 小时前
Swift-static和class
java·服务器·swift