如果一个01串任意两个相邻位置的字符都是不一样的,我们就叫这个01串为交错01串。例如: "1","10101","0101010"都是交错01串。
小易现在有一个01串s,小易想找出一个最长的连续子串,并且这个子串是一个交错01串。小易需要你帮帮忙求出最长的这样的子串的长度是多少。
输入包括字符串s,s的长度length(1 ≤ length ≤ 50),字符串中只包含'0'和'1'
输出一个整数,表示最长的满足要求的子串长度。
111101111
3
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNext()) { String str = in.next(); int maxLen = 1; int len = 1; for (int i = 1; i < str.length(); i++) { if (str.charAt(i - 1) != str.charAt(i)) { len++; if (len > maxLen) { maxLen = len; } } else { len = 1; } } System.out.println(maxLen); } } }
笔试遇到这种送分题一定要做对而且不能浪费时间。。扫一遍O(n)即可
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string s;
int main(){
cin>>s;
int max_len = 1, len = 1;
for(int i=0; i<s.length()-1; i++){
if(s[i+1] != s[i]) {len++; max_len = max(max_len, len);}
else len = 1;
}
cout<<max_len<<endl;
return 0;
}
#!/usr/bin/env python #-*- coding:utf8 -*- def findNum(nums): n = len(nums) if len(set(nums)) == 0: return 0 num , Max = 0, 0 for i in range(1, n): if nums[i] != nums[i-1]: num += 1 else: num = 0 Max = max(Max, num) return Max+1 if __name__ == '__main__': nums = raw_input() print findNum(nums)
#include<iostream> #include <string> using namespace std; int main() { string str; cin >> str; int res = 0; for (int i = 1; i < str.size();) { int k = i-1; while ( i < str.size()) { if (str[i] != str[i - 1]) i++; else break; } if (res < i - k) res = i - k; i++; } cout << res << endl; }
//直接遍历整个字符串,记录每次的长度,并取最大值 while(line = readline()){ var str = line.trim(); var len = str.length; if(len<=1){ console.log(len); }else{ var k=1; var max=1; for(var i=0; i<str.length-1; i++){ if(str[i] !== str[i+1]){ k++; }else{ k=1; } if(k > max){ max = k; } } console.log(max); } }
s=input() num=len(s) s=[int(val) for val in s] for i in range(num-1): s[i]=s[i]+s[i+1] s[-1]=s[-1]+s[-2] count=0 tmp=[] for i in range(num-1): if s[i]==1: count+=1 if s[i]==1 and s[i+1]!=1: tmp.append(count) count=0 if tmp: print(max(tmp)+1) else: print(1)
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine().trim(); System.out.println(solve(str)); } private static int solve(String str) { int left = 0, right = 1; int maxLen = 0; while(right < str.length()) { maxLen = Math.max(maxLen, right - left); // 每次迭代都更新一下最大交错串的长度 if(str.charAt(right) != str.charAt(right - 1)){ // 如果right位置的字符和前一个位置不相同就右移 right ++; if(right == str.length()) break; }else{ // 否则将左指针移动到right,并将right右移一位重新计算后续的交错串长度 left = right; right ++; if(right == str.length()) break; } } // 防止最长交错串以原始字符串的末尾结尾,最后还要更新一下长度 maxLen = Math.max(maxLen, right - left); return maxLen; } }
import java.util.*; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub Scanner cin=new Scanner (System.in); String str=cin.nextLine();//读入字符串 int n=str.length(); int temp=1;//暂时变量 int out=1;//要输出的变量 for(int i=1;i<n;i++) { if(str.charAt(i) !=str.charAt(i-1)) { temp++; // System.out.println(i+" "+str.charAt(i)); } else { out=Math.max(temp, out); temp=1; } } out=Math.max(temp, out); System.out.print(out); } }
#include <iostream> #include <vector> #include <algorithm> using namespace std; // 判断排序后是否构成等差数列 int main(){ string s; cin>>s; // dpa 代表第i个位置以0为结尾连续10串 dpb以1为结尾 int len = s.length(); int ret=0, dpa=0, dpb=0; for(int i=0;i<len;i++){ if(s[i] == '1'){ dpa = dpb + 1; ret = max(ret, dpa); dpb = 0; } else{ dpb = dpa + 1; ret = max(ret, dpb); dpa = 0; } } cout<<ret<<endl; return 0; }
public class Main { public static void main(String args[]){ Scanner sc = new Scanner(System.in); String string = sc.next(); int maxlen = 1; int index = 1; int i = 1; while(i< string.length()){ index = string.charAt(i) == string.charAt(i-1)?1: ++index; maxlen = index>maxlen? index:maxlen; i++; } System.out.println(maxlen); } }
由于已经只确定输入字符只有0和1,所以用一个变量存储前一个字符,然后每次都跟当前字符进行对比看是否不同,最长字串长度设为count,如果相同则count++,否则count更新为1。
import java.util.Scanner; public class Main { public static int process(String str) { if (str.length() < 1) { return 0; } char pre = str.charAt(0); int count = 1; int max = 0; for (int i = 1; i < str.length(); i++) { if (str.charAt(i) != pre) { pre = str.charAt(i); count++; } else { count = 1; } max = Math.max(count, max); } return max; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.nextLine(); System.out.println(process(str)); } }
#其实很简单,将当前输入与前一个输入进行比较, #若不同则子串长度加 1 ; #若相同则舍弃前一个输入,子串从当前输入开始. #include <iostream> using namespace std; int main() { int mid,mid2,max=1,count=1; cin>>mid; while(cin) { cin>>mid2; if(mid2==mid) { count = 1; } else { count++; #记录子串长度 } if(count>max) { max=count; #记录当前最大子串的长度 } mid=mid2; } cout<<max<<endl; return 0; }