PKU_ACM_1035_Spell checker

 1035_Spell checker

 

原题连接

 

简单的字符串操作问题

 

  1. import java.io.*;
  2. import java.util.*;
  3. public class Main
  4. {
  5.     static ArrayList<Item> dicts = new ArrayList<Item>();
  6.     public static void main(String[] args) throws Exception
  7.     {
  8.         readFile();
  9.     }
  10.     static void readFile() throws Exception
  11.     {
  12.         BufferedReader br = new BufferedReader(
  13.             //new FileReader("in.in"));
  14.             new InputStreamReader(System.in));
  15.         StringTokenizer st = null;
  16.         String temp = null;
  17.         int iCount = 1;
  18.         while(!(temp=br.readLine()).equals("#"))
  19.             dicts.add(new Item(iCount++,temp));
  20.         int flag = 0;
  21.         while(!(temp=br.readLine()).equals("#"))
  22.         {
  23.             if(flag!=0)
  24.                 System.out.println();
  25.             process(temp);
  26.             flag++;
  27.         }
  28.     }
  29.     static void process(String str)
  30.     {
  31.         ArrayList<Item> result = new ArrayList<Item>();
  32.         ArrayList<Item> temp = null;
  33.         if(isHave(str))
  34.         {
  35.             System.out.print(str+" is correct");
  36.             return;
  37.         }
  38.         temp = getArrayList(str.length()+1);
  39.         for(Item it : temp)
  40.             if(judge(str,it.str))
  41.                 result.add(it);
  42.         temp = getArrayList(str.length()-1);
  43.         for(Item it : temp)
  44.             if(judge(str,it.str))
  45.                 result.add(it);
  46.         temp = getArrayList(str.length());
  47.         for(Item it : temp)
  48.             if(judge1(str,it.str))
  49.                 result.add(it);
  50.         Collections.sort(result);
  51.         System.out.print(str+":");
  52.         if(result.size()!=0)
  53.             System.out.print(" ");
  54.         int flag = 0;
  55.         for(Item it : result)
  56.         {
  57.             if(flag!=0)
  58.                 System.out.print(" ");
  59.             System.out.print(it.str);
  60.             flag++;
  61.         }
  62.     }
  63.     static boolean judge1(String src,String dest)
  64.     {
  65.         int i = 0;
  66.         int count = 0;
  67.         while(i<src.length())
  68.         {
  69.             if(src.charAt(i)!=dest.charAt(i))
  70.             {
  71.                 count++;
  72.             }
  73.             if(count>1)
  74.                 return false;
  75.             i++;
  76.         }
  77.         return true;
  78.     }
  79.     static boolean judge(String src,String dest)
  80.     {
  81.         String min = dest;
  82.         String max = src;
  83.         if(src.length()<dest.length())
  84.         {
  85.             min = src;
  86.             max = dest;
  87.         }
  88.         int count = 0;
  89.         int i = 0;
  90.         int j = 0;
  91.         while(i<max.length()&&j<min.length())
  92.         {
  93.             if(max.charAt(i)!=min.charAt(j))
  94.             {
  95.                 count++;
  96.                 i++;
  97.             }
  98.             else
  99.             {
  100.                 i++;
  101.                 j++;
  102.             }
  103.             if(count>1)
  104.                 return false;
  105.         }
  106.         return true;
  107.     }
  108.     static boolean isHave(String str)
  109.     {
  110.         for(Item it : dicts)
  111.         {
  112.             if(it.str.equals(str))
  113.                 return true;
  114.         }
  115.         return false;
  116.     }
  117.     static ArrayList<Item> getArrayList(int n)
  118.     {
  119.         ArrayList<Item> result = new ArrayList<Item>();
  120.         for(Item it : dicts)
  121.         {
  122.             if(it.str.length()==n)
  123.                 result.add(it);
  124.         }
  125.         return result;
  126.     }
  127.     static void display()
  128.     {
  129.     }
  130.     static class Item implements Comparable<Item>
  131.     {
  132.         int index;
  133.         String str;
  134.         Item(int i,String s)
  135.         {
  136.             index = i;
  137.             str = s;
  138.         }
  139.         public int compareTo(Item it)
  140.         {
  141.             if(index>it.index)
  142.                 return 1;
  143.             else if(index<it.index)
  144.                 return -1;
  145.             else 
  146.                 return 0;
  147.         }
  148.     }
  149. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值