Hihocoder#1039 : 字符消除 解题记录
2016-07-07 15:21
316 查看
…不知道为什么第一题就出现奇怪的WA….
字符消除 这里是题目的传送门…
感觉就是输入一个字符数组str…然后new 一个字符数组(长度str.length+1),
再根据每个字符增殖一下 例如 ABC → ABBC
..然后再一个循环消除 长度超过1的由相同字母组成的子串 而已。
这里是代码…WA的..测试了很多组数据都发现不了错误…尴尬…最近越来越水了么…
字符消除 这里是题目的传送门…
感觉就是输入一个字符数组str…然后new 一个字符数组(长度str.length+1),
再根据每个字符增殖一下 例如 ABC → ABBC
..然后再一个循环消除 长度超过1的由相同字母组成的子串 而已。
列出最优解 | ABCBCCC | ABABABA |
---|---|---|
1 | 0;ABCCBCCC | 0;ABABBABA |
2 | 5;ABB | 2;ABAABA |
3 | 7;A | 4;ABBA |
4 | 7;A | 6;AA |
5 | 7;A | 8; |
结果为:7 | 结果为:8 |
import java.util.Scanner; public class _2_1039 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); char[] str; for(int i = 0 ; i<n ; i++){ str = input.next().toCharArray(); if(str.length == 0) { System.out.println(0); continue; } byte state; int index = 0; char[] new_str = new char[str.length+1]; int[] res = new int[str.length]; for(int j = 0 ; j < str.length ; j++ ){ if( str[j] == 'D' ){ continue; }else{ state = 0; for(int q = 0; q<new_str.length ;q++){ if(q == j){ new_str[j] = str[j]; new_str[j+1] = str[j]; state = 1; q++; continue; } new_str[q] = str[q-state]; } res[index++] = run(new_str);; } } //求最大值 int max = -1; for(int a : res) if(a > max) max = a; System.out.println(max); } } public static int run( char[] str ){ int temp = -1; int count = 0; int per_index; int per = 'D'; while(temp != 0){ temp = 0; per = 'D'; per_index = -1; for(int j = 0 ; j<str.length ; j++ ){ if(str[j] == 'D'){ continue; }else if(str[j] == per){ temp++; str[j] = 'D'; count++; if(per_index != -1 && str[per_index] != 'D'){ str[per_index] = 'D'; count++; temp++; per_index = -1; } }else{ per = str[j]; per_index = j; } } } return count; } }
相关文章推荐
- 初次使用Android studio 需要的一些配置
- UVA-10943(dp)
- 我用的一些mysql函数--
- DOM编程艺术(数据存储)
- FLAG_ACTIVITY_REORDER_TO_FRONT之ANR问题(Android L)
- CentOS 7 BIND 主从搭建
- windows下关闭TCP的Nagle纳格算法
- CSS基础教程——纯CSS开发的气泡式提示框
- Codeforces Round #361 (Div. 2) C 二分
- 使用Eclipse进行远程调试
- iOS的绘图之drawRect和drawLayer:inContext
- LeetCode题解-226-Invert Binary Tree
- COM中的几个基本概念
- J2V8 For Android
- web开发中浏览器兼容的问题
- Linux常用命令
- 项目开发步骤
- iOS 地图的相关知识《一》
- 链路层
- 【qt】动画按钮---QPropertyAnimation的使用