• UVA-10115


    字符查找替换,WA了N次,一次只能替换一个,下一次find必须从第0个位置开始

    import java.io.File;
    import java.io.FileNotFoundException;
    import java.util.Scanner;
    
    public class Main
    {
        /**
         * 镜像String,通过镜像变换后是回文String 回文String,单纯的回文String
         */
    
        public static void main(String[] args) throws FileNotFoundException
        {
    
            //Scanner scanner = new Scanner(new File("d://1.txt"));
             Scanner scanner = new Scanner(System.in);
            while (scanner.hasNextInt())
            {
                int testCase = scanner.nextInt();
                if (testCase == 0)
                {
                    break;
                }
                String[] find = new String[testCase];
                String[] replace = new String[testCase];
                scanner.nextLine();
                for (int i = 0; i < testCase; i++)
                {
                    find[i] = scanner.nextLine();
                    replace[i] = scanner.nextLine();
                }
                String template = scanner.nextLine();
                for (int i = 0; i < find.length; i++)
                {
                    int index = -1;
                    while ((index = template.indexOf(find[i])) != -1)
                    {
                        String pre = template.substring(0, index);
                        String end = template.substring(index + find[i].length(),
                                template.length());
                        template = pre + replace[i] + end;
                    }
                }
                /// template = findAndReplace(find, replace, template);
                System.out.println(template);
            }
            scanner.close();
        }
    
        public static String findAndReplace(String[] find, String[] replace,
                String template)
        {
            for (int i = 0; i < find.length; i++)
            {
                String f = find[i];
                int fLength = f.length();
                int length = template.length();
                int s = 0, t = 0;
                int fIndex = 0;
                boolean isStart = false;
                for (int j = 0; j < length; j++)
                {
                    if (fIndex == fLength)
                    {
                        t = j;
                        break;
                    }
                    if (template.charAt(j) == f.charAt(fIndex))
                    {
                        if (!isStart)
                        {
                            s = j;
                            isStart = true;
                        }
                        fIndex++;
                        continue;
                    }
                    if (isStart)
                    {
                        j = s;
                        isStart = false;
                        fIndex = 0;
                        continue;
                    }
    
                }
                if (isStart)
                {
                    t = t == 0 ? length : t;
                    StringBuilder sb = new StringBuilder();
                    for (int j = 0; j < s; j++)
                    {
                        sb.append(template.charAt(j));
                    }
                    sb.append(replace[i]);
                    for (int j = t; j < template.length(); j++)
                    {
                        sb.append(template.charAt(j));
                    }
                    template = sb.toString();
                }
                else
                {
                    i++;
                }
            }
            return template;
        }
    
    }
  • 相关阅读:
    python os模块
    python time、datetime模块
    python 导入模块、包
    python json、pickle
    python 装饰器
    python 生成器、迭代器
    python 内置函数
    python 函数的参数
    python 编码解码
    python 文件读写、shutil模块
  • 原文地址:https://www.cnblogs.com/shuiyonglewodezzzzz/p/6262128.html
Copyright © 2020-2023  润新知