poj 1035
来源:互联网 发布:彩虹六号围攻n卡优化 编辑:程序博客网 时间:2024/04/18 17:37
对字符串进行匹配。
按题目要求,可以进行四种形式的匹配。
1.不动原串。
2给原串增加一个letter。
3替换原串中的一个letter。
4删除原串中的一个letter。
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.Collections;public class Main {static ArrayList<Item> dicts = new ArrayList<Item>();public static void main(String[] args)throws Exception{readFile();}public static void readFile()throws Exception{ //读取数据BufferedReader br = new BufferedReader(new InputStreamReader(System.in));String temp = null;int iCount = 1;while(!(temp = br.readLine()).equals("#")){dicts.add(new Item(iCount++, temp)); //给字典编号了,并且字典还实现了序列化的接口}//for(Item it:dicts){//System.out.println(it.index+" "+it.str);//}int flag = 0;while(!(temp = br.readLine()).equals("#")){//每读入一个数据,对它进行操作if(flag!=0){System.out.println();}process(temp); //读这些数据进行操作flag++;}}public static void process(String str){ArrayList<Item> result = new ArrayList<Item>();ArrayList<Item> temp = null;if(isHava(str)){System.out.print(str+" is correct");return;}temp = getArrayList(str.length()+1); //这种情况是对插入一个字符的模拟for(Item it:temp){if(judge(str, it.str)){result.add(it);}}temp = getArrayList(str.length()-1);//删除一个字符的模拟for(Item it:temp){if(judge(str, it.str)){result.add(it);}}temp = getArrayList(str.length());//替换一个字符的模拟for(Item it:temp){if(judge1(str,it.str)){result.add(it);}}Collections.sort(result); //实现序列化接口,为了是把符合要求的元素按给定的字典序输出//for(Item it:result){//System.out.println(it.index+" "+it.str);//}System.out.print(str+":");if(result.size()!=0){System.out.print(" ");}int flag = 0;for(Item it:result){if(flag!=0){System.out.print(" ");}System.out.print(it.str);flag++;}}public static boolean judge1(String src, String dest){ //很好的模拟啊~经典就应该多看啊int i = 0;int count = 0;while(i < src.length()){if(src.charAt(i) != dest.charAt(i)){count++;}if(count>1)return false;i++;}return true;}public static boolean judge(String src, String dest){ //很好的模拟啊,真的挺好的String min =dest;String max = src;if(src.length() < dest.length()){//找到长度长短的min = src;max = dest;}int count = 0;int i = 0;int j = 0;while(i < max.length() && j<min.length()){if(max.charAt(i)!=min.charAt(j)){count++;i++;}else{i++;j++;}if(count>1)return false;}return true;}public static boolean isHava(String str){for(Item it:dicts){if(it.str.equals(str)){return true;}}return false;}public static ArrayList<Item> getArrayList(int n){ArrayList<Item> result = new ArrayList<Item>();for(Item it:dicts){if(it.str.length() == n){result.add(it);}}return result;}public static class Item implements Comparable<Item>{int index;String str;Item(int i, String s){index = i;str = s;}public int compareTo(Item it){if(index > it.index){return 1;}else if(index < it.index){return -1;}else{return 0;}}}}
- POJ 1035
- Poj 1035
- POJ 1035
- poj 1035
- poj 1035
- poj 1035
- poj 1035
- POJ 1035
- poj.1035
- poj-1035
- poj 1035
- poj 1035
- poj 1035
- POJ 1035
- POJ 1035
- poj 1035
- poj-1035-Spell checker
- poj 1035 Spell checker
- 黑马程序员—java数组学习回顾
- Android在文本中添加超链接 (TextView)
- outlook各种问题一路下来
- 优秀的程序员
- win7设置JDK环境变量
- poj 1035
- 基于Java多线程的下载器源码剖析(三)
- Hadoop执行DistributedGrep
- foxmail替代outlook完成邮件管理、时间管理(谷歌日历同步)、rss订阅
- 从设计的角度讨论Java中线程的两种创建方式
- 开源软件清除了“开源”和“商业”之间的障碍——商业软件、开源软件和自由软件的区别
- 用单链表实现stack
- 树表查找之二叉排序树
- tbr tbn tbc