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 == )
{
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的更多相关文章
- UVa 10115 Automatic Editing
字符串题目就先告一段落了,又是在看balabala不知道在说些什么的英语. 算法也很简单,用了几个库函数就搞定了.本来还担心题里说的replace-by为空的特殊情况需要特殊处理,后来发现按一般情况处 ...
- 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 ...
- uva 1354 Mobile Computing ——yhx
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABGcAAANuCAYAAAC7f2QuAAAgAElEQVR4nOy9XUhjWbo3vu72RRgkF5
- UVA 10564 Paths through the Hourglass[DP 打印]
UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- UVA&&POJ离散概率与数学期望入门练习[4]
POJ3869 Headshot 题意:给出左轮手枪的子弹序列,打了一枪没子弹,要使下一枪也没子弹概率最大应该rotate还是shoot 条件概率,|00|/(|00|+|01|)和|0|/n谁大的问 ...
- UVA计数方法练习[3]
UVA - 11538 Chess Queen 题意:n*m放置两个互相攻击的后的方案数 分开讨论行 列 两条对角线 一个求和式 可以化简后计算 // // main.cpp // uva11538 ...
- 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 ...
- UVA - 1625 Color Length[序列DP 代价计算技巧]
UVA - 1625 Color Length 白书 很明显f[i][j]表示第一个取到i第二个取到j的代价 问题在于代价的计算,并不知道每种颜色的开始和结束 和模拟赛那道环形DP很想,计算这 ...
- UVA - 10375 Choose and divide[唯一分解定理]
UVA - 10375 Choose and divide Choose and divide Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
随机推荐
- 51Nod 1091:线段的重叠(贪心)
1091 线段的重叠 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题 收藏 关注 X轴上有N条线段,每条线段包括1个起点和终点.线段的重叠是这样来算的,[10 ...
- Fzu软工第二次作业-词频分析
(0)前言: Github项目 作业地址 (1)PSP表格: PSP2.1 Personal Software Process Stages 预估耗时(分钟) 实际耗时(分钟) Planning 计划 ...
- 【mysql】mac上基于tar.gz包安装mysql服务
一.准备工作 (1)下载mysql-5.7.21-macos10.13-x86_64.tar.gz,并将该压缩包移动至/usr/local目录下 (2)解压压缩包 二.安装 (1)将解压的包重命名为m ...
- gitlab操作
一.初始设置 在某一个具体的project下: 1.gitlab中删除一个工程Setting-->General-->Advanced settings-->RemoveProjec ...
- oracle 以及 sql server mysql 空值默认值修改
在SQL Server Oracle MySQL当数据库中查出某值为NULL怎么办? 1.MSSQL: ISNULL() 语法 ISNULL ( check_expression , replacem ...
- FastAdmin 开发第三天:安装 FastAdmin
环境安装安装好后就可以安装 FastAdmin 了. 根据文档说明安装步骤如下,推荐使用命令行安装: 克隆FastAdmin到你本地 git clone https://git.oschina.net ...
- php实现cookie加密解密
1.加密解密类 class Mcrypt { /** * 解密 * * @param string $encryptedText 已加密字符串 * @param string $key 密钥 * @r ...
- golang init函数
init函数有一下几点特性: init函数在main执行之前,自动被调用执行的,不能显示调用 每个包的init函数在包被引用时,自动被调用 每个包可以有多个init函数 同一个文件中可定义多个init ...
- opencv中的缩放函数
图像处理里面缩放操作是比较常见的: 最近邻插值:类似简单映射的处理方式,目标图像w1,h1,原始图像w0,h0,则在目标图像上的点(x,y)的像素点实际对应原始图上(x*w0/w1,y*h0/h1)的 ...
- MySQL 中间件 mycat 的使用
什么是MYCAT 一个彻底开源的,面向企业应用开发的大数据库集群 支持事务.ACID.可以替代MySQL的加强版数据库 一个可以视为MySQL集群的企业级数据库,用来替代昂贵的Oracle集群 一个融 ...