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 ...
随机推荐
- 王垠:完全用Linux工作 - imsoft.cnblogs
完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...
- java设计模——反射的应用 (利用反射来去除if判断语句)
利用反射来去除if判断语句 我的以前写的一个查分系统,就是部长让我写的那个,使用一个分发器(函数),他会根据传递进来的字符串参数调用不同的方. If(“add”.equalsIgnoreCase(fu ...
- hdu1097
hdu1097 求a^b的末位数 打表O(1) import java.util.*; public class Main { static int [][]a = new int[15][15]; ...
- 关于Hibernate性能优化之 FetchType=Lazy时查询数据
当表A和表B一对多的关系 对于A和B的实体类,设置FetchType=EAGER时,取A表数据,对应B表的数据都会跟着一起加载,优点不用进行二次查询.缺点是严重影响数据查询的访问时间. 解决办法Fet ...
- nyoj 密码宝盒
密码宝盒 时间限制:2000 ms | 内存限制:65535 KB 难度:3 描述 小M得到了一个宝盒,可惜打开这个宝盒需要一组神奇的密码,然而在宝盒的下面 有关于密码的提示信息:密码是一个C进制 ...
- js中如何快速获取数组中的最大值最小值
var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math.min.apply(null, a));//最小值 多维数组可以这么修 ...
- MySQL 瓶颈及应对措施
注:内容摘抄自<PHP 核心技术与最佳实践>一书 MySQL 是存在瓶颈的. 当 MySQL 单表数据量达到千万级别以上时,无论如何对 MySQL 进行优化,查询如何简单,MySQL 的性 ...
- phper必知必会之类库自动加载的七种方式(三)
## php自动加载 下面显示例子的文件目录结构图 一.没有使用命名空间的几种实现 test/oneClass.php class oneClass{ public function show(){ ...
- 更新上篇文章 调用三级目录文章内容 dede频道页实现三级栏目嵌套调用文章
原文:http://www.wuaie.com/?p=66 源码改写 $typeid = $row['id']; if((class_exists('PartView'))) { $pv = new ...
- C#使用WebService 常见问题处理
C#使用WebService 一.新建webservice 新建项目→asp.net Web服务应用程序 或者在现有项目中 点击右键 新建web服务程序asmx 只要在webservice类里面 ...