小明得到一个只包含a,b两个字符的字符串,但是小明不希望在这个字符串里a出现在b左边。现在他可以将”ab”这样的子串替换成”bba”,在原串中的相对位置不变。输出小明最少需要操作多少次才能让一个给定字符串所有a都在b的右边。
小明得到一个只包含a,b两个字符的字符串,但是小明不希望在这个字符串里a出现在b左边。现在他可以将”ab”这样的子串替换成”bba”,在原串中的相对位置不变。输出小明最少需要操作多少次才能让一个给定字符串所有a都在b的右边。
一个只包含a,b字符的字符串,长度不超过100000。
最小的操作次数。结果对1000000007取模。
ab
1
ab到bba
aab
3
aab到abba到bbaba到bbbbaa
#include <bits/stdc++.h> using namespace std; int main() { ios::sync_with_stdio(0),cin.tie(0); int i,j; long long bnum=0,ans=0,mod=1000000007; string s; cin>>s; for(i=s.size()-1;i>=0;i--) { if(s[i]=='b') bnum++; else if(s[i]=='a') { ans=(ans+bnum)%mod; bnum=bnum*2%mod; } } cout<<ans; return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { static final int MOD = 1000000007; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); char[] str = br.readLine().trim().toCharArray(); int countB = 0; int res = 0; for(int i = str.length - 1; i >= 0; i--){ if(str[i] == 'b'){ countB ++; }else{ // 此时需要替换 res = (res + countB) % MOD; countB = countB * 2 % MOD; } } System.out.println(res); } }
Max = 1000000007 s = input() nb = 0 res = 0 for i in range(len(s)-1, -1, -1): if s[i] == 'a': res += nb nb *= 2 else: nb += 1 print(res%Max)累加b的数量即可
import java.util.*; import java.io.*; import java.math.*; import java.text.*; public class Main{ static int mod=(int)1e9+7; public static void main(String args[]) throws IOException{ Read sc=new Read(); //int n=sc.nextInt(); String s=sc.next(); List<Integer> list=new ArrayList<>(); long ans=0; long sum=0; for(int i=s.length()-1;i>=0;i--){ if(s.charAt(i)=='b'){ sum++; } else{ ans=(ans+sum)%mod; sum=(sum*2)%mod; } } sc.print(ans); sc.bw.flush(); sc.bw.close(); } } //记住看数字范围,需要开long吗,需要用BigInteger吗,需要手动处理字符串吗,复杂度数量级控制在1e7或者以下了吗 //开数组的数据范围最高不能超过1e7,数据范围再大就要用哈希表离散化了 //基本数据类型不能自定义sort排序,二维数组就可以了,顺序排序的时候是小减大,注意返回值应该是int class Read{ BufferedReader bf; StringTokenizer st; BufferedWriter bw; public Read(){ bf=new BufferedReader(new InputStreamReader(System.in)); st=new StringTokenizer(""); bw=new BufferedWriter(new OutputStreamWriter(System.out)); } public String nextLine() throws IOException{ return bf.readLine(); } public String next() throws IOException{ while(!st.hasMoreTokens()){ st=new StringTokenizer(bf.readLine()); } return st.nextToken(); } public char nextChar() throws IOException{ // 确定下一个@可爱抱抱 只有一个字符的时候再用 return next().charAt(0); } public int nextInt() throws IOException{ return Integer.parseInt(next()); } public long nextLong() throws IOException{ return Long.parseLong(next()); } public double nextDouble() throws IOException{ return Double.parseDouble(next()); } public float nextFloat() throws IOException{ return Float.parseFloat(next()); } public byte nextByte() throws IOException{ return Byte.parseByte(next()); } public short nextShort() throws IOException{ return Short.parseShort(next()); } public BigInteger nextBigInteger() throws IOException{ return new BigInteger(next()); } public void println(int a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(int a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(String a) throws IOException{ bw.write(a); bw.newLine(); return; } public void print(String a) throws IOException{ bw.write(a); return; } public void println(long a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(long a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(double a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } public void print(double a) throws IOException{ bw.write(String.valueOf(a)); return; } public void print(BigInteger a) throws IOException{ bw.write(a.toString()); return; } public void print(char a) throws IOException{ bw.write(String.valueOf(a)); return; } public void println(char a) throws IOException{ bw.write(String.valueOf(a)); bw.newLine(); return; } }
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.*; //f[i][j] 表示从 i - j 这一段变成回文串的最小代价 /* 转移方程举例子:f[i][j] = Math.min(f[i][j], f[i][j - 1] + Math.min( inc[ c[j] - '0' ] , dec[ c[j] - '0' ] ));为可以这样 我们是枚举len长度下的最下代价,也已知了 j = i + len - 1也就是i-j长度就是len的 这部分使我们要去求的
但是f[i][j - 1]的最小代价我们是上一步 len - 1长度枚举的时候就已经求了 f[i + 1][j - 1]是len - 2的长度,当然我们更早求了
因此有 c[i] == c[j] 时候 我们f[i + 1][j - 1]早就算出了, 那么必然有f[i][j] = f[i + 1][j - 1]这样才最小
但是当c[i] != c[j] 举例子 xxxxx1 反正其他的不管这个5个x = xxxxx我们早就知道了最小代价。那么多了一个1
直接删除变成xxxxxx 或者在添加一个1 -> 1xxxxxx1 也就是:f[i][j] = Math.min(f[i][j], f[i][j - 1] + Math.min( inc[ c[j] - '0' ] , dec[ c[j] - '0' ] ));
*/ public class Main { static String t = ""; static int N = 110, INF = (int)10e9; static int[][] f = new int[N][N]; static int[] inc = new int[]{-1, 100,200,360,220}; static int[] dec = new int[]{-1, 120,350,200,320}; public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); t = bf.readLine(); char[] c = t.toCharArray(); for(int i = 0; i < c.length; i ++) { Arrays.fill(f[i], INF); f[i][i] = 0; } for(int len = 2; len <= c.length; len ++) //枚举区间长度 for(int i = 0; i + len - 1 < c.length; i ++){ //枚举左边端点 int j = i + len - 1; //右边端点 if(c[i] == c[j] ){ f[i][j] = len == 2 ? 0 : f[i + 1][j - 1]; }else if(c[i] != c[j]){ f[i][j] = Math.min(f[i][j], f[i][j - 1] + Math.min( inc[ c[j] - '0' ] , dec[ c[j] - '0' ] )); f[i][j] = Math.min(f[i][j], f[i + 1][j] + Math.min( inc[ c[i] - '0' ] , dec[ c[i] - '0' ] )); } } System.out.println(f[0][c.length - 1]); } }
#include <bits/stdc++.h> using namespace std; typedef long long ll; ll quick_power(ll a, ll n, int mod=1000000007){ if(n == 0){ return 1; } if(n == 1){ return a; } ll temp = a * a % mod; if(n % 2 == 1){ return a * quick_power(temp, (n - 1) / 2) % mod; } else{ return quick_power(temp, n / 2) % mod; } } int main(){ ll res = 0; int index = 0, a_count = 0, mod = 1000000007; string s; cin >> s; while(s[index] == 'b'){ index ++; } while(index < s.size()){ while(index < s.size() && s[index] == 'a'){ a_count ++; index ++; } if(index == s.size()){ break; } // cout << index << " " << a_count << endl; ll temp = quick_power(2, a_count) - 1; // cout << temp << endl; res = (res + temp) % mod; index ++; } cout << res << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main() { string s("aab"); int res=0, tmp =0; int len = s.size(); for (int i = len - 1; i >= 0; i--){ if (s[i] == 'b') { tmp++; } else if(s[i] == 'a'){ res = res + tmp; tmp = tmp * 2; } } cout << res << endl; return res; }
#include<bits/stdc++.h> using namespace std; #define ll long long const int N=1e5+100; const int mod=1e9+7; char ss[N]; int main(){ ios::sync_with_stdio(false); cin>>ss+1; int n=strlen(ss+1); ll cnt=0;//累积b个数 ll ans=0;//答案 for(int i=n;i;i--){ if(ss[i]=='b') cnt++; else{ ans=(ans+cnt)%mod; cnt=cnt*2%mod;//当a移动过去了,其后面的b会多一倍 } } cout<<ans<<endl; return 0; }