今天这场都是字符串,打到自闭┭┮﹏┭┮
A - You Are Given Two Binary Strings…
找出第二个字符串最后面1的位置,然后再从第一个字符前面往前找第一个1出现的位置。
#include <iostream>
#include <cmath>
using namespace std ;
int main(){
int t ;
cin >> t ;
while(t --){
string a , b ;
cin >> a >> b ;
int x = 0 ;
for (int i = b.length()-1 ; i >= 0 ; -- i){
if (b[i] == '1') break ;
++ x ;
}
int ans = 0 ;
for (int i = a.length()-x-1 ; i >= 0 ; -- i){
if (a[i] == '1') break ;
++ ans ;
}
cout << ans << endl ;
}
return 0 ;
}
B - You Are Given a Decimal String…
#include <iostream>
#include <string>
#include <cstring>
using namespace std ;
const int INF = 0x3f3f3f3f ;
const int N = 10 ;
int dis[N][N] , ans[N][N] ;
string s ;
int cal(int x , int y){ //计算 x-y计算器
memset(dis,INF,sizeof(dis)) ;
for (int i = 0 ; i < 10 ; ++ i){
dis[i][(i+x)%10] = 1 ; //对于i它只能转移到(i+x)%10 或者是(i+y)%10 的点 其他无法到达为INF
dis[i][(i+y)%10] = 1 ;
}
// 计算从 i 到 j 的最短路
for (int k = 0 ; k < 10 ; ++ k)
for (int i = 0 ; i < 10 ; ++ i)
for (int j = 0 ; j < 10 ; ++ j)
if (dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j] ;
int ans = 0 ;
int len = s.length() ;
for (int i = 0 ; i < len-1 ; ++ i){
int a = s[i] - '0' ;
int b = s[i+1] - '0' ;
if (dis[a][b] == INF) return -1 ;
else
ans += dis[a][b] - 1 ; //累加从s[i] 到 s[i+1] 中间要插入几个数
}
return ans ;
}
int main(){
cin >> s ;
for (int i = 0 ; i < 10 ; ++ i){
for (int j = 0 ; j < 10 ; ++ j){
cout << cal(i,j) << " " ;
}
cout << endl ;
}
return 0 ;
}
D - Print a 1337-string…
题意:给出n要求输出一串字符串中含有n个“1337” ;
题解:中间有n个3 则有(c n中取2中组合方式,即x*(x-1)/2 种),如果没在这个范围内可以每隔两个3插入一个7,就会多出一种。所以找出x*(x-1)/2 最接近n的x,然后不够的插入7即可。
#include <cstdio>
int main(){
int t ;
scanf ("%d",&t) ;
while(t --){
int n ;
scanf ("%d",&n) ;
if (n == 1){
printf ("1337\n") ;
continue ;
}
int x = 1 ; //x为中间 3 的个数
while(x*(x-1)/2 <= n) ++ x ;
-- x ;
n = n - x*(x-1)/2 ;
printf ("133") ;
while(n --) printf ("7") ;
x -= 2 ;
while(x --) printf ("3") ;
printf ("7\n") ;
}
return 0 ;
}