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 ...
随机推荐
- 在intent-filter中的配置
1.scheme约束和mimetype的数据类型,这些都可以自己去定义. 2.但是由于在MainActivity却不能直接将这两个参数分开来写,例如setdata和settype,这样会互相删除. 因 ...
- jenkins构建配置
# Poll SCM:定时检查源码变更(根据SCM软件的版本号),如果有更新就checkout最新code下来,然后执行构建动作 # 每5分钟检查一次源码变化 # Build periodically ...
- Hadoop学习笔记(1)(转)
Hadoop学习笔记(1) ——菜鸟入门 Hadoop是什么?先问一下百度吧: [百度百科]一个分布式系统基础架构,由Apache基金会所开发.用户可以在不了解分布式底层细节的情况下,开发分布式程序. ...
- MySQL--忘记MYSQL管理员密码
如root用户密码,可以按照以下方式来修改: STEP1: 停止MySQL服务 ps -ef | grep -v 'grep' | grep 'mysqld' | awk '{print $2}' | ...
- 【模式识别】MPL,MIL和MCL
Multi-Instance Learning (MIL) 和Multi-Pose Learning (MPL)是CV的大牛Boris Babenko at UC San Diego提出来的.其思想能 ...
- lseek成功但未生效?
如果open打开文件时,指定了O_APPEND,即“追加”模式,那么lseek的向前移动指针的操作无法凑效,包括lseek(fd, 负数, SEEK_CUR)和lseek(fd, 小于当前偏移的位置, ...
- TensorFlow 官方文档中文版学习
TensorFlow 官方文档中文版 地址:http://wiki.jikexueyuan.com/project/tensorflow-zh/
- Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧)
干货:Source Insight 4 中文乱码的解决办法(source insight 3.5 及以下版本就到其他地方看看吧) [解决办法]: 菜单栏中[File]->[Reload As E ...
- Oracle ADDM性能诊断利器及报告解读
性能优化是一个永恒的话题,性能优化也是最具有价值,最值得花费精力深入研究的一个课题,因为资源是有限的,时间是有限的.在Oracle数据库中,随着Oracle功能的不断强大和完善,Oralce数据库在性 ...
- Microsoft Dynamics CRM4.0 创建单据的时候,自动生成单据编号的通用方法
一.新建两个实体,具体如下: 单据流水号(new_maxbillcode) 显示名称 名称 类型 格式 最大长度 需求级别 IME模式 备注 名称 new_name nvarchar 文本 100 业 ...