字符查找替换,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. Unity 3D的常用快捷键

    Unity中的常用快捷键 本文提供全流程,中文翻译.Chinar坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) 1 Windows系统Unity3 ...

  2. Gym 101745 D.Stamp Stamp Stamp

    题目网页链接: http://codeforces.com/gym/101745/problem/D 思路:首先可以确保能够成功染色的字符串都是结果串的子串,那么O(n^2)枚举子串之后dp转移即可. ...

  3. Koa下http代理

    前言 最近做管理后台的重构或者说重做. 至于为什么要重构. 随意的解释: 是原来写的人走了. 客观的解释: 用的人觉得不好用 维护的人员找不到北 再多一点解释: express + ejs的混合编写 ...

  4. hdu2098 分拆素数和 素数筛

    将一个偶数拆成两个素数的和,欧拉筛暴力 #include<stdio.h> #include<string.h> #define N 10001 ]; ]; ,j; void ...

  5. arduino 配置 esp8266

    在连接之前,先把程序下载到arduino中,很简单,就是定义了软口.如果中间要改动程序,要把rx和tx的连线去掉,不然下载程序可能失败. ; ; void setup() { pinMode(rx,I ...

  6. lsof-查看进程句柄

    root@root:~# lsof COMMAND PID TID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd root cwd DIR , / sy ...

  7. Socket-Vs-WebSocket-TestTool

    项目地址 :  https://github.com/kelin-xycs/Socket-Vs-WebSocket-TestTool Socket-Vs-WebSocket-TestTool 一个用 ...

  8. webpack externals

    当我们想在项目中require一些其他的类库或者API,而又不想让这些类库的源码被构建到运行时文件中,这在实际开发中很有必要.此时我们就可以通过配置externals参数来解决这个问题: //webp ...

  9. [BZOJ5249][九省联考2018]IIIDX(线段树)

    5249: [2018多省省队联测]IIIDX Time Limit: 40 Sec  Memory Limit: 512 MBSubmit: 32  Solved: 17[Submit][Statu ...

  10. C#实现不安装Oracle客户端访问远程服务器数据

    概述: C#通过使用ADO的方式在未安装Oracle数据库的前提下,客户端程序远程访问服务器,会出现:“System.Data.OracleClient 需要 Oracle 客户端软件 8.1.7 或 ...