字符查找替换,WA了N次,一次只能替换一个,下一次find必须从第0个位置开始

  1. import java.io.File;
  2. import java.io.FileNotFoundException;
  3. import java.util.Scanner;
  4.  
  5. public class Main
  6. {
  7. /**
  8. * 镜像String,通过镜像变换后是回文String 回文String,单纯的回文String
  9. */
  10.  
  11. public static void main(String[] args) throws FileNotFoundException
  12. {
  13.  
  14. //Scanner scanner = new Scanner(new File("d://1.txt"));
  15. Scanner scanner = new Scanner(System.in);
  16. while (scanner.hasNextInt())
  17. {
  18. int testCase = scanner.nextInt();
  19. if (testCase == )
  20. {
  21. break;
  22. }
  23. String[] find = new String[testCase];
  24. String[] replace = new String[testCase];
  25. scanner.nextLine();
  26. for (int i = ; i < testCase; i++)
  27. {
  28. find[i] = scanner.nextLine();
  29. replace[i] = scanner.nextLine();
  30. }
  31. String template = scanner.nextLine();
  32. for (int i = ; i < find.length; i++)
  33. {
  34. int index = -;
  35. while ((index = template.indexOf(find[i])) != -)
  36. {
  37. String pre = template.substring(, index);
  38. String end = template.substring(index + find[i].length(),
  39. template.length());
  40. template = pre + replace[i] + end;
  41. }
  42. }
  43. /// template = findAndReplace(find, replace, template);
  44. System.out.println(template);
  45. }
  46. scanner.close();
  47. }
  48.  
  49. public static String findAndReplace(String[] find, String[] replace,
  50. String template)
  51. {
  52. for (int i = ; i < find.length; i++)
  53. {
  54. String f = find[i];
  55. int fLength = f.length();
  56. int length = template.length();
  57. int s = , t = ;
  58. int fIndex = ;
  59. boolean isStart = false;
  60. for (int j = ; j < length; j++)
  61. {
  62. if (fIndex == fLength)
  63. {
  64. t = j;
  65. break;
  66. }
  67. if (template.charAt(j) == f.charAt(fIndex))
  68. {
  69. if (!isStart)
  70. {
  71. s = j;
  72. isStart = true;
  73. }
  74. fIndex++;
  75. continue;
  76. }
  77. if (isStart)
  78. {
  79. j = s;
  80. isStart = false;
  81. fIndex = ;
  82. continue;
  83. }
  84.  
  85. }
  86. if (isStart)
  87. {
  88. t = t == ? length : t;
  89. StringBuilder sb = new StringBuilder();
  90. for (int j = ; j < s; j++)
  91. {
  92. sb.append(template.charAt(j));
  93. }
  94. sb.append(replace[i]);
  95. for (int j = t; j < template.length(); j++)
  96. {
  97. sb.append(template.charAt(j));
  98. }
  99. template = sb.toString();
  100. }
  101. else
  102. {
  103. i++;
  104. }
  105. }
  106. return template;
  107. }
  108.  
  109. }

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. 王垠:完全用Linux工作 - imsoft.cnblogs

    完全用Linux工作,抛弃windows 我已经半年没有使用 Windows 的方式工作了.Linux 高效的完成了我所有的工作. GNU/Linux 不是每个人都想用的.如果你只需要处理一般的事务, ...

  2. java设计模——反射的应用 (利用反射来去除if判断语句)

    利用反射来去除if判断语句 我的以前写的一个查分系统,就是部长让我写的那个,使用一个分发器(函数),他会根据传递进来的字符串参数调用不同的方. If(“add”.equalsIgnoreCase(fu ...

  3. hdu1097

    hdu1097 求a^b的末位数 打表O(1) import java.util.*; public class Main { static int [][]a = new int[15][15]; ...

  4. 关于Hibernate性能优化之 FetchType=Lazy时查询数据

    当表A和表B一对多的关系 对于A和B的实体类,设置FetchType=EAGER时,取A表数据,对应B表的数据都会跟着一起加载,优点不用进行二次查询.缺点是严重影响数据查询的访问时间. 解决办法Fet ...

  5. nyoj 密码宝盒

    密码宝盒 时间限制:2000 ms  |  内存限制:65535 KB 难度:3 描述 小M得到了一个宝盒,可惜打开这个宝盒需要一组神奇的密码,然而在宝盒的下面 有关于密码的提示信息:密码是一个C进制 ...

  6. js中如何快速获取数组中的最大值最小值

    var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Math.min.apply(null, a));//最小值 多维数组可以这么修 ...

  7. MySQL 瓶颈及应对措施

    注:内容摘抄自<PHP 核心技术与最佳实践>一书 MySQL 是存在瓶颈的. 当 MySQL 单表数据量达到千万级别以上时,无论如何对 MySQL 进行优化,查询如何简单,MySQL 的性 ...

  8. phper必知必会之类库自动加载的七种方式(三)

    ## php自动加载 下面显示例子的文件目录结构图 一.没有使用命名空间的几种实现 test/oneClass.php class oneClass{ public function show(){ ...

  9. 更新上篇文章 调用三级目录文章内容 dede频道页实现三级栏目嵌套调用文章

    原文:http://www.wuaie.com/?p=66 源码改写 $typeid = $row['id']; if((class_exists('PartView'))) { $pv = new ...

  10. C#使用WebService 常见问题处理

    C#使用WebService   一.新建webservice 新建项目→asp.net Web服务应用程序 或者在现有项目中 点击右键 新建web服务程序asmx 只要在webservice类里面 ...