字符查找替换,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 == )
{
break;
}
String[] find = new String[testCase];
String[] replace = new String[testCase];
scanner.nextLine();
for (int i = ; i < testCase; i++)
{
find[i] = scanner.nextLine();
replace[i] = scanner.nextLine();
}
String template = scanner.nextLine();
for (int i = ; i < find.length; i++)
{
int index = -;
while ((index = template.indexOf(find[i])) != -)
{
String pre = template.substring(, 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 = ; i < find.length; i++)
{
String f = find[i];
int fLength = f.length();
int length = template.length();
int s = , t = ;
int fIndex = ;
boolean isStart = false;
for (int j = ; 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 = ;
continue;
} }
if (isStart)
{
t = t == ? length : t;
StringBuilder sb = new StringBuilder();
for (int j = ; 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;
} }

UVA-10115的更多相关文章

  1. UVa 10115 Automatic Editing

    字符串题目就先告一段落了,又是在看balabala不知道在说些什么的英语. 算法也很简单,用了几个库函数就搞定了.本来还担心题里说的replace-by为空的特殊情况需要特殊处理,后来发现按一般情况处 ...

  2. UVA大模拟代码(白书训练计划1)UVA 401,10010,10361,537,409,10878,10815,644,10115,424,10106,465,10494

    白书一:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=64609#overview 注意UVA没有PE之类的,如果PE了显示WA. UVA ...

  3. uva 1354 Mobile Computing ——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5

  4. UVA 10564 Paths through the Hourglass[DP 打印]

    UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...

  5. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  6. UVA&&POJ离散概率与数学期望入门练习[4]

    POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...

  7. UVA计数方法练习[3]

    UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...

  8. UVA数学入门训练Round1[6]

    UVA - 11388 GCD LCM 题意:输入g和l,找到a和b,gcd(a,b)=g,lacm(a,b)=l,a<b且a最小 g不能整除l时无解,否则一定g,l最小 #include &l ...

  9. UVA - 1625 Color Length[序列DP 代价计算技巧]

    UVA - 1625 Color Length   白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束   和模拟赛那道环形DP很想,计算这 ...

  10. UVA - 10375 Choose and divide[唯一分解定理]

    UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. CodeForces - 1087F:Rock-Paper-Scissors Champion(set&数状数组)

    n players are going to play a rock-paper-scissors tournament. As you probably know, in a one-on-one ...

  2. mysql-python安装时mysql_config not found

    在安装 mysql-python时,会出现: sh: mysql_config: not found Traceback (most recent call last):   File "s ...

  3. 网络流--最小费用最大流MCMF模板

    标准大白书式模板 #include<stdio.h> //大概这么多头文件昂 #include<string.h> #include<vector> #includ ...

  4. js 时间操作积累

    console.log( new Date() ); //Tue Mar 20 2018 22:47:01 GMT+0800 (中国标准时间) // var date = new Date( '201 ...

  5. 【知识笔记】ASP.NET报错问题

    一.前端asp.net在远程部署到IIS时报错:Web 部署任务失败,在远程计算机上处理请求时出错 服务器在处理请求时遇到问题.请与服务器管理员联系以了解详细信息((2017/6/16 16:22:4 ...

  6. streamsets 3.5 的一些新功能

    streamsets 3.5 有了一些新的特性以及增强,总之是越来越方便了,详细的可以 查看官方说明,以下简单例举一些比较有意义的. origins 新的pulsar 消费origin jdbc 多表 ...

  7. folder,source folder,package 区别

    source folder source folder下面的Java文件都会被编译,编译后的文件会被放在我们设置的某个文件夹下面(一般我们设置成WEB-INF/classes),source fold ...

  8. python 函数参数的传递(参数带星号的说明) 元组传递 字典传递

    python中函数参数的传递是通过赋值来传递的.函数参数的使用又有俩个方面值得注意:1.函数参数是如何定义的 2.在调用函数的过程中参数是如何被解析 先看第一个问题,在python中函数参数的定义主要 ...

  9. MSCRM 2011中过滤化查询的实现方法和禁用选择视图

    MS CRM 2011中过滤化查询的实现方法 使用过滤化查询,可以实现根据表单上的其他信息作为搜索条件,限制Lookup查找框中的搜索结果数量的目的. 在讲解具体的实现方法之前,先设定一个应用场景: ...

  10. pbuf类型和应用

    下面的讨论仅限于RAW API. 按存储方式分类 1. PBUF_RAM 从一般性的Heap中分配.可用空间大小受MEM_SIZE宏控制.可看作一般意义上的动态内存. 用途: a) 将应用层中的待发送 ...