输入数据包括n+1行:
第一行为一个整数n(1 ≤ n ≤ 50),即棋盘的大小
接下来的n行每行一个字符串表示第i行棋盘的颜色,'W'表示白色,'B'表示黑色
输出小易会涂画的区域大小
3 BWW BBB BWB
3
package go.jacob.day914;
import java.util.Scanner;
/**
* [编程题]涂棋盘
* @author Administrator
* 循环的时候要注意,是求每一列连续相同颜***域
*/
public class Demo4 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] strs = new String[n];
for (int i = 0; i < n; i++) {
strs[i] = sc.next();
}
int max = 0;
for (int i = 0; i < n; i++) {
int count = 1;
for (int j = 1; j < n; j++) {
if (strs[j].charAt(i) == strs[j-1].charAt(i)) {
count++;
} else {
if (count > max)
max = count;
count = 1;
}
}
if (count > max)
max = count;
}
System.out.println(max);
sc.close();
}
}
import java.util.Scanner;
/*
* 这题就是求各数组相同字符子串的最大长度
* */
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
String[] str = new String[n];
for (int i = 0; i < n; i++) {
str[i] = scan.next();
}
int count = 0;
for (int j = 0; j < n; j++) {
int c = 1;
for (int i = 0; i < n - 1; i++) {
if (str[i].charAt(j) == str[i + 1].charAt(j)) {
c++;
count = Math.max(c, count);
} else {
c = 1;
}
}
}
System.out.println(count);
}
scan.close();
}
}
package drawing;
import java.util.*;
/**
* 这个题求的是某一列中拥有相同颜色最多的区域,其实把这道题目分解开,算法模型就是求一个数组中相同元素最多的子数组长度。
* 比如说,数组为[1, 1, 1, 2, 2, 2, 2, 3],那么最大长度就是4。
* 怎么求呢?
* 建立一个辅助数组dp,里面存放的是到这个数为止,相同元素最多的子数组的长度。
* 那么dp的值为[1, 2, 3, 1, 2, 3, 4, 1]。
* 我们只需要保存这个数组的最大值返回即可。
* 那么这道题就是每一列为一个数组,求每一列的相同元素最多的子数组长度,然后找出最大值返回即可。
* @author 何嘉龙
*
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while (in.hasNext()) {
int n = in.nextInt();
char[][] area = new char[n][n];
for (int i = 0; i < n; i++) {
char[] chas = in.next().toCharArray();
for (int j = 0; j < n; j++) {
area[i][j] = chas[j];
}
}
System.out.println(getMaxArea(area, n));
}
in.close();
}
public static int getMaxArea(char[][] arr, int n) {
int maxArea = 0;
for (int i = 0; i < n; i++) {
int count = 1;
int maxCount = 1;
int[] dp = new int[n];
dp[0] = 1;
for (int j = 1; j < n; j++) {
if (arr[j][i] == arr[j - 1][i]) {
count++;
dp[j] = count;
} else {
count = 1;
dp[j] = count;
}
if (dp[j] > maxCount) {
maxCount = dp[j];
}
}
if (maxCount > maxArea) {
maxArea = maxCount;
}
}
return maxArea;
}
}
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main()
{
using namespace std;
int n;
while (cin >> n) {
vector<vector<char> > matrix(n, vector<char>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> matrix[i][j];
}
}
int max = 0;
for (int i = 0; i < n; i++) {
int ret = 1;
for (int j = 0; j < n - 1; j++) {
if (matrix[j][i] == matrix[j + 1][i]) {
ret++;
if (ret > max) {
max = ret;
}
}
else {
ret = 1;
}
}
}
cout << max << endl;
}
return 0;
}
#include <stdio.h>
#define N 50
int max(int a, int b);
int main(){
int n,i,j; //棋盘的大小
char str[N][N];
scanf("%d",&n);
getchar();
for(i=0;i<n;i++){
gets(str[i]);
}
int max_num=0;
for(j=0;j<n;j++){ //j是列
int w_num=0,b_num=0;
for(i=0;i<n;i++){ // i是行
if(i==0){
w_num=1; b_num=1;
}else{
if(str[i][j]==str[i-1][j]){
if(str[i][j]=='W') w_num++;
if(str[i][j]=='B') b_num++;
if(i==n-1){
max_num = max(max(b_num,w_num), max_num);
w_num=0;b_num=0;
}
}else{
max_num = max(max(b_num,w_num), max_num);
w_num=1; b_num=1;
}
}
}
}
printf("%d",max_num);
return 0;
}
int max(int a, int b){
return a > b ? a : b;
}
纯C写~~~
n, *bw = open(0).read().split() n = int(n) def lamp(grid): B = [1 if i == 'B' else 0 for i in grid[0]] W = [1 if i == 'W' else 0 for i in grid[0]] m = max(max(B), max(W)) for j in range(1, n): tb = [1 if i == 'B' else 0 for i in grid[j]] tw = [1 if i == 'W' else 0 for i in grid[j]] B = [B[i] + 1 if tb[i] == 1 else 0 for i in range(n)] W = [W[i] + 1 if tw[i] == 1 else 0 for i in range(n)] m = max(m, max(B), max(W)) return m print(max(lamp(bw), lamp(list(zip(*bw))))) # 才发现题目只需要看列就行了。。。python的特色zip不能不用,如果有numpy这题可以写的更优美点
import sys class Solution: def draw_color(self, matrix): col = 0 max_count = 0 while col < len(matrix): pre = '' tmp_count = 0 for row in range(len(matrix)): if matrix[row][col] == pre: tmp_count += 1 max_count = max([tmp_count, max_count]) else: pre = matrix[row][col] max_count = max([tmp_count, max_count]) tmp_count = 1 if max_count == len(matrix): print(max_count) return col += 1 print(max_count) if __name__ == '__main__': n = int(sys.stdin.readline().strip()) mat = list() for _ in range(n): mat.append(sys.stdin.readline().strip()) solution = Solution() solution.draw_color(mat)
import java.util.Scanner;
/*
* 相对简单的贪心
* */
public class BWTuban {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
int n = scanner.nextInt();
String[] strings = new String[n];
for (int i = 0; i < n; i++) {
strings[i] = scanner.next();
}
int max = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
// 当前列,白色和黑色连续最大值
int bCount = 0;
int wCount = 0;
for (int j = 0; j < n; j++) {
if(strings[j].charAt(i)=='B'){
int bTmp=1;
while(j+1<n&&strings[j+1].charAt(i)=='B'){
bTmp++;
j++;
}
// 更新
bCount = Math.max(bCount,bTmp);
}
if(strings[j].charAt(i)=='W'){
int wTmp=1;
while(j+1<n&&strings[j+1].charAt(i)=='W'){
wTmp++;
j++;
}
wCount = Math.max(wCount,wTmp);
}
}
// 更新
max = Math.max(max,Math.max(bCount,wCount));
}
System.out.println(max);
}
}
}
//判断当前字符是否与上一个last相等
//相等subMax++ 否则subMax置为1
//记得更新last
import java.util.Scanner;
public class Main1 { public static int cal2(int[][] nums) { int max=0; for(int i=0; i<nums.length; i++) { int last = nums[0][i]; int subMax=1; for(int j=1; j<nums.length; j++) { if(last == nums[j][i]) { subMax+=1; if(subMax>max) max=subMax; }else { subMax=1; } last = nums[j][i]; } } return max; } public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); s.nextLine(); int[][] nums = new int[n][n]; for (int i = 0; i < n; i++) { String str = s.nextLine(); for (int j = 0; j < n; j++) { char c = str.charAt(j); nums[i][j] = c; } } System.out.println(cal2(nums)); s.close(); }
}
//注意count定义的位置,如果定义在for外层,则通过率只有80%
import java.util.Scanner;
public class test9 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
String [] strings = new String[n];
for(int i = 0;i < n;i++){
strings[i] = scanner.next();
}
int max = Integer.MIN_VALUE;
for(int j = 0;j < n;j++){
int count = 1;
for(int i = 0;i < n-1;i++){
char temp = strings[i].charAt(j);
if(strings[i+1].charAt(j) == temp){
count++;
max = Math.max(count, max);
//if(count > max){
//System.out.println("the lie is " + j);
//}
}
else {
count = 1;
}
}
}
System.out.println(max);
}
}
#include<iostream>
#include<vector>
using namespace std;
int main()
{
int maxLen = 0;
string s;
int n;
cin>>n;
vector<vector<char>> vvc(n,vector<char>(n));
for(int i=0;i<n;i++) {
cin>>s;
for(int j=0;j<n;j++)
vvc[i][j]=s[j];
}
for(int j=0;j<n;j++) {
int curLen = 1;
for(int i=1;i<n;i++)
if(vvc[i][j]==vvc[i-1][j]) {
if(++curLen>maxLen)
maxLen = curLen;
}
else
curLen = 1;
}
cout<<maxLen;
}
#include <iostream>
using namespace std;
int update(int len) {
static int maxLen = 0;
if (len > maxLen) {
maxLen = len;
}
return maxLen;
}
int main(int argc, const char * argv[]) {
int n;
cin>>n;
char board[n][n];
for (int i = 0; i < n; i++) {
cin>>board[i];
}
for (int i = 0; i < n; i++) {
int seriesLen = 1;
for (int j = 0; j < n - 1; j++) {
if (board[j][i] == board[j + 1][i]) {
seriesLen++;
} else {
update(seriesLen);
seriesLen = 1;
}
}
if (seriesLen != 1) {
update(seriesLen);
}
}
cout<<update(0);
return 0;
}
# -*- coding:utf-8 -*-n = int(raw_input())s = [[] fori in range(n)]fori in range(n):si = raw_input()forj in range(n):s[i].append(si[j])#s = np.array(s)无numpyss = [[]fori in range(n)]fori in range(n):forj in range(n):ss[i].append(s[j][i])s = ss#计算每一列的最长连续子串counts = []fori in range(n):a = 0counter = 1ci = s[i]ifa+1< n:forj in range(a+1,n):ifci[j] == ci[a]:counter += 1ifj == n-1:a += countercounts.append(counter)breakifci[j] != ci[a]:counts.append(counter)a += countercounter = 1print max(counts)
#include<iostream>#include<vector>#include<algorithm>#include<string>using namespace std;intmain(){intn;cin >> n;vector<string> v(n, " ");for(inti = 0; i < n; ++i){cin >> v[i];}//计算每一列'B'的个数intres = 0;for(inti = 0; i < n; ++i){intnum = 1;for(intj = 1; j < n; ++j){if(v[j][i] == v[j - 1][i]){num ++;}else{res = max(res, num);num = 1;}}res = max(res, num);}cout << res << endl;}
#include <iostream>
#include <vector>
#include <memory.h>
using namespace std;
int main(){
int n;
cin>>n;
vector<vector<char>> data;
for (int i = 0;i<n;++i)
{
vector<char> vecTemp;
for (int j = 0;j<n;++j)
{
char temp;
cin>>temp;
vecTemp.push_back(temp);
}
data.push_back(vecTemp);
}
int out = 0;
for (int j = 0;j<n;++j)
{
int maxBlack = -1;
int maxWhite = -1;
int blackCount = 0;
int whiteCount = 0;
for (int i = 0;i<n;++i)
{
if(data[i][j] == 'B'){
blackCount++;
maxWhite = max(maxWhite,whiteCount);
whiteCount = 0;
}
else
{
whiteCount++;
maxBlack = max(maxBlack,blackCount);
blackCount = 0;
}
}
maxBlack = max(maxBlack,blackCount);
maxWhite = max(maxWhite,whiteCount);
out = max(out,maxBlack);
out = max(out,maxWhite);
}
cout<<out;
return 0;
} #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int count(vector<vector<char>> &p,int n){
int numw=0,numb=0,maxw=0,maxb=0;
char pre;
if(p[0][n]=='W')
numw++,pre='W';
else
numb++,pre='B';
for(int i=1;i<p.size();i++){
if(p[i][n]==pre){
if(pre=='W'){
numw++;
}
else{
numb++;
}
}
else{
maxw=max(numw,maxw);
maxb=max(numb,maxb);
if(p[i][n]=='W'){
numw=1;
numb=0;
}
else{
numw=0;
numb=1;
}
pre=p[i][n];
}
}
maxw = max(numw, maxw);
maxb = max(numb, maxb);
return max(maxb,maxw);
}
int main(){
int n;
cin>>n;
vector<vector<char>> p;
for(int i=0;i<n;i++){
vector<char> t(n,'0');
for(int j=0;j<n;j++){
cin>>t[j];
}
p.push_back(t);
}
int maxc=0;
for(int i=0;i<n;i++){
maxc=max(maxc,count(p,i));
}
cout<<maxc<<endl;
return 0;
}