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 ...
随机推荐
- PS如何批量处理图片
喜爱摄影的朋友可能都有这样的体会,相机里面存了大量的图片,一般都是2048×1536或者更大像素的照片,每张都有1M以上,如果设置的清晰度高,则照片就更大,这样的图片是无法上传到博客中的(博客要求每张 ...
- day30 python学习 struct模块和 subprocess 模块
import subprocess import struct aa=input('>>') obj=subprocess.Popen(aa,shell=True,#aa代表的是读取字符串 ...
- xsl -fo 了解
XSL-FO是用于格式化XML数据的语言,全称为Extensible Stylesheet Language Formatting Objects(格式化对象的可扩展样式表语言),是W3C参考标准,现 ...
- oracle 查某一列有重复值的记录
-- 查找重复记录select names,num from test where rowid != (select max(rowid) from test b ...
- PHP安装sqlsrv和memcache扩展步骤
做了两天的实验才终于摸清楚如何将PHP连接上sqlserver数据库,以及怎样通过修改virtualhost文件来重定向,因为走得弯路比较多所以很想分享一下这次的心路历程. 第一步:安装wamp等类似 ...
- dup and dup2的剖析
转:http://www.cnblogs.com/sdphome/archive/2011/04/30/2033381.html dup和dup2都可用来复制一个现存的文件描写叙述符,使两个文件描写叙 ...
- sqlserver中numeric字段截取
方法一:convert(float,字段名) as 别名 select convert(float,round(10.123232,2)) 结果:10.12 select convert(float, ...
- ResourceBundle介绍
介绍: ResourceBundle类主要作用是读取属性文件,读取属性文件时可以直接指定属性文件的名称(指定名称时不需要文件的后缀),也可以根据Locale所指定的区域码来选取指定的资源文件: Res ...
- 什么是Asp.net Core?和 .net core有什么区别?(转)
什么是Asp.Net core 我相信很多C# Developer已经对于.net core不算陌生了,就算没有正式使用相信也应该有所了解.微软在推出来.net core的同时为了方便一些原有的项目可 ...
- FineUI 3升级4.1.1时,SingleClickExpand属性改什么了? (树控件单击展开)
private Tree InitTreeMenu(List<Menu> menus) { Tree treeMenu = new Tree(); treeMenu.ID = " ...