全部评论
只a了第一道,第二道不想考虑掉落了 pass 60%😥
消消乐 ac代码 (C++) #include<iostream>
#include<algorithm>
#include<vector>
#include<unordered_map>
using namespace std;
int res=0;
bool check(vector<vector<char> >& ditu)
{
vector<vector<int>> jilu;
int m = ditu.size();
int n = ditu[0].size();
bool xiaochu = false;
//先从行找
for(int i=0;i<m;i++)
{
for(int j=0;j<=n-3;)
{
if(ditu[i][j] == '0')
{
j++;
continue;
}
int k=j;
while(k<n&&ditu[i][k]==ditu[i][j]) k++;
if(k-j>=3)
{
vector<int> onejilu{i,j,i,k-1};
jilu.push_back(onejilu);
xiaochu = true;
}
j = k;
}
}
//从列开始找
for(int i=0;i<n;i++)
{
for(int j=0;j<=m-3;)
{
if(ditu[j][i] == '0')
{
j++;
continue;
}
int k=j;
while(k<m&&ditu[k][i] == ditu[j][i]) k++;
if(k-j>=3)
{
vector<int> onejilu{j,i,k-1,i};
jilu.push_back(onejilu);
xiaochu = true;
}
j=k;
}
}
//消除
for(auto one:jilu)
{
int x1=one[0],y1=one[1],x2=one[2],y2=one[3];
if(x1==x2)
{
for(int i=y1;i<=y2;i++)
ditu[x1][i] = '0';
}
else //y1==y2
{
for(int i=x1;i<=x2;i++)
ditu[i][y1] = '0';
}
}
return xiaochu;
}
void down(vector<vector<char> >& ditu)
{
int m = ditu.size();
int n = ditu[0].size();
//从第一行开始
for(int i=1;i<m;i++)
{
for(int j=0;j<n;j++)
{
if(ditu[i][j] == '0')
{
//cout<<"in down"<<endl;
int cengshu = i;
while(cengshu-1>=0&&ditu[cengshu-1][j]!='0')
{
ditu[cengshu][j] = ditu[cengshu-1][j];
cengshu--;
}
ditu[cengshu][j] = '0';
}
}
}
}
int main()
{
int m,n;
cin>>m>>n;
int x1,y1,x2,y2;
vector<vector<char> > ditu(m,vector<char>(n));
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
cin>>ditu[i][j];
cin>>x1>>y1>>x2>>y2;
int orizero=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(ditu[i][j]=='0')
orizero++;
swap(ditu[x1][y1],ditu[x2][y2]);
down(ditu);
while(check(ditu))
down(ditu);
int nowzero=0;
for(int i=0;i<m;i++)
for(int j=0;j<n;j++)
if(ditu[i][j] =='0')
nowzero++;
cout<<nowzero-orizero<<endl;
}
第一道的代码。 import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
StringBuilder str= new StringBuilder();
// while (in.) {// 注意,如果输入是多个测试用例,请通过while循环处理多个测试用例
str.append(in.nextLine());
// System.out.println(str.toString());
// }
char[] chs=str.toString().toCharArray();
solution(chs,0);//处理方法,0是多余的。不用管
int maxNum=0;//结果最大值
String maxStr="";//结果的字符
for (int i=65;i<=90;i++){
if(res[i]+res[i+32]>maxNum){
maxNum=res[i]+res[i+32];//a和A一起算,ASCII码差32
maxStr=String.valueOf((char)(i+32));
}
}
StringBuilder sb=new StringBuilder();
sb.append(maxStr);
sb.append(maxNum);
System.out.println(sb.toString());
}
public static int[] res=new int[150];//结果集。index是字符的ASCII值,value是字符出现次数
public static void solution(char[] chs,int start){
if(start>chs.length){
return;
}
StringBuilder sb=new StringBuilder();//存字符
StringBuilder num=new StringBuilder();//存后面重复次数
for (;start<chs.length;start++){
if(chs[start]+0<=57 && chs[start]+0>=48){//是数字的话
num.append(chs[start]);
if(start+1<=chs.length-1 && chs[start+1]+0<=57 && chs[start+1]+0>=48){
continue;//边界处理
}
input(sb.toString(),num.toString());//计算重复次数
sb=new StringBuilder();//重置
num= new StringBuilder();
continue;
}
sb.append(chs[start]);//字符直接添加
}
}
public static void input(String s,String num){
char[] cs=s.toCharArray();//这里是字符
int m=Integer.parseInt(num);//这里是字符后的数字
for (int i=0;i<cs.length;i++){
if(cs[i]=='-'){//处理-出现
for(int j=cs[i-1]+1;j<=cs[i+1];j++){
if(j==91){
j=97;//跳过中间的无效字符
}
res[j] =res[j]+m;//这个字符的重复次数
}
}
res[cs[i]] =res[cs[i]]+m;//处理一般字符
}
}
}
// 代码写的很乱 A了
// 大家看看就好,轻喷
#include "bits/stdc++.h"
#include "unordered_map"
using namespace std;
stack<char> stk;
unordered_map<char,int> book; // 字母映射表 char -> num
unordered_map<int,int> times; // 字母(num)出现的次数
void initMap(){
int cnt=0;
// 0-25 大写字母
for(char ch='A';ch<='Z';ch++){
book[ch]=cnt;
cnt++;
}
// 26-41 小写
for(char ch='a';ch<='z';ch++){
book[ch]=cnt;
cnt++;
}
cnt=50;
// 50-59 数字
for(char ch='0';ch<='9';ch++){
book[ch]=cnt;
cnt++;
}
// '-' 100
book['-']=100;
}
// 取出栈中元素 并重复指定的次数
string compute(int n){
string part="";
string res="";
while(!stk.empty()){
part=stk.top()+part;
stk.pop();
}
for(int i=0;i<n;i++){
res+=part;
}
return res;
}
// 统计字符出现的次数
void getTimes(string &s){
int m=0;
for(int i=0;i<s.size();i++){
// 0-25 大写字母
// 26-41 小写字母
if(book[s[i]]<50){
int num=book[s[i]];
times[num]++;
if(times[num]>m){
m=times[num];
}
// 统一归类到大写字母
if(num>=26){
times[num-26]++;
if(times[num-26]>m){
m=times[num-26];
}
}
}
}
// 按字母序遍历大写字母
for(int i=0;i<26;i++){
if(times[i]==m){
// 转换成小写字母
char ch=(char)(i+97);
cout<<ch<<times[i];
break;
}
}
return ;
}
int main(){
initMap();
string s="";
cin>>s;
//cout<<s<<endl;
string res="";
string num="";
for(int i=0;i<s.size();i++){
// 字母
if(book[s[i]]<50){
// 这里表示把数字都读完了,开始计算
if(num!=""){
int n=stoi(num);
string part=compute(n);
res+=part;
num="";
}
stk.push(s[i]);
continue;
}
// 数字
if(book[s[i]]>=50 && book[s[i]]<=59){
num +=s[i];
continue;
}
// '-'
if(book[s[i]]==100){
char ch=stk.top();
stk.pop();
for(char c=ch; book[c]<=book[s[i+1]];c++){
if(book[c])
stk.push(c);
}
i++;
}
}
// 把栈中剩余的字符处理一下
if(num!=""){
int n=stoi(num);
string part=compute(n);
res+=part;
num="";
}
// cout<<res<<endl;
getTimes(res);
return 0;
}
顺便求offer 😥
暴力法全A了😂
package 米哈游;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
//A3bc2X-b2
public class 压缩字符串 {
static HashMap<Character,Integer> map = new HashMap<>();
static HashMap<Character,Integer> tempMap = new HashMap<>();
static int max = 0;
static char maxCh ='z'+1;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String line = sc.nextLine();
Character pre = null,next = null;
for(int i = 0 ; i < line.length() ; i++) {
char ch = line.charAt(i);
//如果不是字母
if(ch<'A'||ch>'z') {
//下划线
if(ch=='-') {
pre = line.charAt(i-1);
next = line.charAt(++i);
// System.out.println("pre"+pre);
// System.out.println("pre"+next);
for(int j = pre ; j <=next ; j++) {
if(j>'Z'&&j<'a') continue;
int val = 0;
if(tempMap.containsKey((char)j)) val = tempMap.get((char)j);
tempMap.put((char)j, val+1);
//System.out.println((char)j+"---"+(val+1));
}
}
//数字
else {
int num = Integer.parseInt(ch+"");//倍数
cal(num);
//重置缓存
tempMap = new HashMap<>();
}
}
//字母
else {
int val = 0;
if(tempMap.containsKey(ch)) val = tempMap.get(ch);
tempMap.put(ch, val+1);
pre = ch;
}
}
cal(1);
System.out.println(""+maxCh+max);
}
public static void cal(int num) {
Iterator<Character> chs = tempMap.keySet().iterator();
//遍历缓存,合并到map中
while(chs.hasNext()) {
int val = 0,newVal = 0;
char tempCh = chs.next();
if(map.containsKey(tempCh)) val = map.get(tempCh);
newVal = val+tempMap.get(tempCh)*num;
//System.out.println(tempCh+":"+tempMap.get(tempCh)+":"+num+":"+newVal);
if(newVal==0) newVal++;
map.put(tempCh, newVal);
if(tempCh<'a') {
int v = 0;
char BtempCh = (char)(tempCh+('a'-'A'));
//System.out.println("BtempCh:"+BtempCh);
if(map.containsKey(BtempCh)) v = map.get(BtempCh);
newVal += v;
//System.out.println(map.get(BtempCh));
tempCh = BtempCh;
//System.out.println(v);
}else {
int v = 0;
char StempCh = (char)(tempCh-('a'-'A'));
//System.out.println("StempCh:"+StempCh);
if(map.containsKey(StempCh)) v = map.get(StempCh);
newVal += v;
//System.out.println(map.get(StempCh));
//tempCh = StempCh;
//System.out.println(v);
}
//更新最大值
if(newVal>=max) {
if(tempCh<maxCh) {
max = newVal;
maxCh = tempCh;
}
}
}
}
}
我太难了,一道都没写出来,这道题还是结束后20分钟才调出来的,,,,,好惨啊
数字要考虑多位数的情况 a99 a199 这种 考虑到就A了
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
while (cin >> str) {
for (auto &it : str) {
it = tolower(it);
}
cout << str << endl;
string res;
string tmp;
int i = 0;
int cnt = 0;
while(i<str.size()){
if (isdigit(str[i])) {
cnt = stoi(str.substr(i));
while (i<str.size()&&isdigit(str[i]))
++i;
while (cnt--) {
res += tmp;
}
tmp = "";
}
else if (str[i] == '-') {
char pre = str[i - 1];
char nxt = str[i + 1];
char ptr = pre;
tmp.pop_back();
while (ptr != nxt) {
tmp += ptr;
if (ptr == 'z')
ptr = 'a';
else
++ptr;
}
tmp += nxt;
i += 2;
}
else {
tmp += str[i++];
}
}
cout << res << endl;
vector<int> dp(26, 0);
int max_count = 0;
for (auto it : res) {
++dp[it - 'a'];
max_count = max(max_count, dp[it - 'a']);
}
for(int i=0;i<dp.size();++i)
if (dp[i] == max_count) {
char ch = i + 'a';
cout << ch << dp[i] << endl;
break;
}
}
return 0;
}
import java.util.*;
public class P1 {
public static void main(String[] args) {
// String s = "A3bc2X-b2";
String s = new Scanner(System.in).nextLine();
System.out.println(solution1(s));
}
private static String solution1(String s) {
s += "#4";//保证最后一次尾部可以获取到
List<Character> cyc = new ArrayList<>();
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.delete(0, stringBuffer.length());
TreeMap<Character, Long> map = new TreeMap<>(new Comparator<Character>() {
@Override
public int compare(Character o1, Character o2) {
return o1-o2;
}
});
boolean flag = true;
int i = 0;
while (i<s.length()){
char cur = s.charAt(i);
if(!flag){
long count = Long.parseLong(stringBuffer.toString());
stringBuffer.delete(0, stringBuffer.length());
if(cyc.contains('-')){
char start = cyc.get(0);
char end = cyc.get(2);
for (char j = start; j <=end ; j++) {
char key = j;
if('A'<=key&&key<='Z')
key = (char)(key + 32);
if(map.containsKey(key)){
map.put(key, map.get(key) + count);
}
else {
map.put(key, count);
}
}
}
else {
for (Character c:cyc
) {
char key = c;
if('A'<=key&&key<='Z')
key = (char)(key + 32);
if(map.containsKey(key)){
map.put(key, map.get(key) + count);
}
else {
map.put(key, count);
}
}
}
cyc.clear();
flag = true;
}
else if(flag&&'0'<=cur&&cur<='9'){
stringBuffer.append(cur);
i++;
}
else if(flag&&!cyc.isEmpty()&&stringBuffer.length()>0&&!('0'<=cur&&cur<='9')){
flag = false;//开始统计
}
else {
flag = true;
cyc.add(cur);
i++;
}
}
//从map中获取最大频次的char和count
long max = Long.MIN_VALUE;
char maxc = ' ';
for (char c:map.keySet()
) {
if(map.get(c)>max){
max = map.get(c);
maxc = c;
}
}
String res = String.valueOf(maxc) + String.valueOf(max);
return res;
}
}
老哥们,米哈游后端能用python写么
定义两个数组,一个计算当前字符数目,一个计算总体(每遇到数字计算一次),AC了,第二题随便写了下0.2就赶紧做后台附加题了
第一题直接暴力法混个分,第二题60%不知道哪有问题。
第一道80第二道60,感觉不太好调,直接去做最后额外的题目了。。
就过了60% 求大佬私聊AC代码 实在不知道哪里错了啊
我也是,我太菜了
解压字符串怎么搞
相关推荐
06-18 18:23
成都大学 Java 点赞 评论 收藏
分享