#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
string s , t , ans;
int n , m;
int cnts_1 , cntt_1 , cnts_0 , cntt_0 ;
int nex[N];
void init(string s){
int len = s.size();
nex[1] = 0;
for(int i = 2 ; i <= len ; i ++){
nex[i] = nex[i-1];
while(nex[i] && s[nex[i]] != s[i-1]) nex[i] = nex[nex[i]];
nex[i] += (s[i-1] == s[nex[i]]);
}
}
/*
1010101100
10101
*/
signed main(){
cin >> s >> t;
n = s.size();
m = t.size();
init(t);
int pos = nex[m];
string now = t.substr(pos);
for(int i = 0 ; i < n ; i ++) {
cnts_0 += 1 * (s[i] == '0');
cnts_1 += 1 * (s[i] == '1');
}
for(int i = 0 ; i < m ; i ++) {
cntt_0 += 1 * (t[i] == '0');
cntt_1 += 1 * (t[i] == '1');
}
if(cnts_0 < cntt_0 || cnts_1 < cntt_1) {
cout << s << "\n";
} else {
cnts_0 -= cntt_0;
cnts_1 -= cntt_1;
ans += t;
m = now.size();
cntt_0 = cntt_1 = 0;
for(int i = 0 ; i < m ; i ++) {
cntt_0 += 1 * (now[i] == '0');
cntt_1 += 1 * (now[i] == '1');
}
int need = INF;
if(cntt_0) need = min(need , cnts_0 / cntt_0);
if(cntt_1) need = min(need , cnts_1 / cntt_1);
for(int i = 1 ; i <= need ; i ++) {
ans += now;
}
cnts_1 -= need * cntt_1;
cnts_0 -= need * cntt_0;
for(int i = 1 ; i <= cnts_1 ; i ++) ans += '1';
for(int i = 1 ; i <= cnts_0 ; i ++) ans += '0';
cout << ans << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
能表示出面的全集即可
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
map<PII,PII>mp;
//[面 , id , val]
int n , a[N] , b[N] , c[N] , res , id;
PII ans , now;
signed main(){
IOS
cin >> n;
res = -INF;
for(int i = 1 ; i <= n ; i ++) {
cin >> a[i] >> b[i] >> c[i];
if(min({a[i] , b[i] , c[i]}) > res) {
res = min({a[i] , b[i] , c[i]});
id = i;
}
}
bool tag = 0 ;
for(int i = 1 ; i <= n ; i ++) {
now = {a[i] , b[i]};
if(mp.find(now) != mp.end()) {
if(min({c[i] + mp[now].se , a[i] , b[i]}) > res) {
tag = 1;
res = min({c[i] + mp[now].se , a[i] , b[i]});
ans = {i , mp[now].fi};
}
}
now = {b[i] , a[i]};
if(mp.find(now) != mp.end()) {
if(min({c[i] + mp[now].se , a[i] , b[i]}) > res) {
tag = 1;
res = min({c[i] + mp[now].se , a[i] , b[i]});
ans = {i , mp[now].fi};
}
}
now = {a[i] , c[i]};
if(mp.find(now) != mp.end()) {
if(min({b[i] + mp[now].se , a[i] , c[i]}) > res) {
tag = 1;
res = min({b[i] + mp[now].se , a[i] , c[i]});
ans = {i , mp[now].fi};
}
}
now = {c[i] , a[i]};
if(mp.find(now) != mp.end()) {
if(min({b[i] + mp[now].se , a[i] , c[i]}) > res) {
tag = 1;
res = min({b[i] + mp[now].se , a[i] , c[i]});
ans = {i , mp[now].fi};
}
}
now = {b[i] , c[i]};
if(mp.find(now) != mp.end()) {
if(min({a[i] + mp[now].se , b[i] , c[i]}) > res) {
tag = 1;
res = min({a[i] + mp[now].se , b[i] , c[i]});
ans = {i , mp[now].fi};
}
}
now = {c[i] , b[i]};
if(mp.find(now) != mp.end()) {
if(min({a[i] + mp[now].se , b[i] , c[i]}) > res) {
tag = 1;
res = min({a[i] + mp[now].se , b[i] , c[i]});
ans = {i , mp[now].fi};
}
}
if(c[i] > mp[{a[i] , b[i]}].se) {
mp[{a[i] , b[i]}] = {i , c[i]};
}
if(c[i] > mp[{b[i] , a[i]}].se) {
mp[{b[i] , a[i]}] = {i , c[i]};
}
if(b[i] > mp[{a[i] , c[i]}].se) {
mp[{a[i] , c[i]}] = {i , b[i]};
}
if(b[i] > mp[{c[i] , a[i]}].se) {
mp[{c[i] , a[i]}] = {i , b[i]};
}
if(a[i] > mp[{b[i] , c[i]}].se) {
mp[{b[i] , c[i]}] = {i , a[i]};
}
if(a[i] > mp[{c[i] , b[i]}].se) {
mp[{c[i] , b[i]}] = {i , a[i]};
}
}
if(!tag){
cout << "1\n";
cout << id << "\n";
}else{
cout << "2\n";
cout << ans.fi << " " << ans.se << "\n";
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);
时间复杂度 O ( 26 n m 2 T ) 时间复杂度O(26nm^2T) 时间复杂度O(26nm2T)
#include
using namespace std;
#define fi first
#define se second
#define IOS std::ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
#define int long long
const int INF = 9e18;
const int N = 2e6 + 10;
const int mod = 1e9 + 7;
typedef pair<int,int>PII;
int t , n , m;
string s[20] , x , y , ans;
bool judge(string now){
for(int i = 1 ; i <= n ; i ++) {
int res = 0;
for(int j = 0 ; j < m ; j ++) {
if(now[j] != s[i][j]) res += 1;
}
if(res > 1) return 0;
}
return 1;
}
signed main(){
IOS
cin >> t;
while(t --) {
cin >> n >> m;
for(int i = 1 ; i <= n ; i ++) cin >> s[i];
x = s[1];
bool tag = 0;
for(int i = 0 ; i < m ; i ++) {
y = x;
for(char j = 'a' ; j <= 'z' ; j ++) {
y[i] = j;
if(judge(y)) {
tag = 1;
ans = y;
}
}
}
if(tag) {
cout << ans << "\n";
} else {
cout << "-1\n";
}
}
return 0;
}
//freopen("文件名.in","r",stdin);
//freopen("文件名.out","w",stdout);x`x`