题目链接

题意:英语很简单,自己取读吧。

思路:

既然n和i字符串的长度都很小,最大才50,那么就是只要能出答案就任意暴力瞎搞。

本人本着暴力瞎搞的初衷,写了又臭又长的200多行(代码框架占了50行)。反正不忘初衷就对了。

暴力:枚举每一个字符串,然后暴力去算其他字符串变成该字符串需要用多少步骤,然后在众多答案中取最小值。

需要注意的是答案是-1的情况,需要用到字符串的最小表示法,预处理进行把所有的字符串变成最小表示法的字符串,如果有不一样的,那么就输出-1。因为可以通过首位对接搞成的字符串的最小表示法是唯一的。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb std::ios::sync_with_stdio(false)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define gg(x) getInt(&x)
using namespace std;
typedef long long ll;
inline void getInt(int* p);
const int maxn=;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
char s[][];
char b[][];
int bj[];
int fbj[];
void findMin(char* s)
{
int len =strlen(s);
int i=,j=,k=;
while(i<len&&k<len&&j<len)
{
if(s[(i+k)%len]==s[(j+k)%len])
{
k++;
}
else if(s[(i+k)%len]>s[(j+k)%len])
{
i=i+k+;
k=;
}
else
{
j=j+k+;
k=;
}
if(i==j) j++;
}
int tt=min(i,j);// findMin里的
char str[];
strcpy(str,s);
for(i=;i<len;i++)
{
s[i]=str[(tt+i)%len];
}
s[len]='\0';
}
int main()
{
gbtb;
cin>>n;
repd(i,,n)
{
cin>>s[i]; }
int len=strlen(s[]);
repd(i,,len-)
{
bj[s[][i]]++;
}
repd(i,,n)
{
MS0(fbj);
repd(j,,len-)
{
fbj[s[i][j]]++;
}
repd(j,,)
{
if(fbj[j]!=bj[j])
{
cout<<-<<'\n';
return ;
}
}
}
repd(i,,n)
{
repd(j,,len-)
{
b[i][j]=s[i][j];
}
b[i][len]='\0';
findMin(b[i]);
}
repd(i,,n-)
{
if(strcmp(b[i],b[n])!=)
{
cout<<-<<'\n';
return ;
}
}
int ans=0x3f3f3f3f;
repd(i,,n)
{ int num=;
repd(j,,n)
{
if(i!=j)
{
int cnt=;
int deng=;
int f=;
for(int k=;k<len;k++)
{
if(s[i][f]!=s[j][k])
{
if(deng)
{ cnt+=deng;
deng=;
f=;
k--;
continue;
}
// if(k+1<len&&s[i][f]!=s[j][k+1])
cnt++;
}else
{
f++;
deng++;
}
}
num+=cnt;
}
}
ans=min(ans,num);
}
cout<<ans<<'\n';
return ;
} inline void getInt(int* p) {
char ch;
do {
ch = getchar();
} while (ch == ' ' || ch == '\n');
if (ch == '-') {
*p = -(getchar() - '');
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * - ch + '';
}
}
else {
*p = ch - '';
while ((ch = getchar()) >= '' && ch <= '') {
*p = *p * + ch - '';
}
}
}

Mike and strings CodeForces - 798B (又水又坑)的更多相关文章

  1. Mike and strings CodeForces - 798B (简洁写法)

    题目链接 时间复杂度 O(n*n*|s| ) 纯暴力,通过string.substr()函数来构造每一个字符串平移后的字符串. #include <iostream> #include & ...

  2. Mike and strings 798B

    B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  3. Codeforces Round #410 (Div. 2) B. Mike and strings

    B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. CF410div2 B. Mike and strings

    /* CF410div2 B. Mike and strings http://codeforces.com/contest/798/problem/B 字符串 暴力 题意:给你n个串,每次操作可以将 ...

  5. 牛客OI周赛8-提高组A-用水填坑

    牛客OI周赛8-提高组A-用水填坑 题目 链接: https://ac.nowcoder.com/acm/contest/403/A 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制: ...

  6. 【codeforces 798B】Mike and strings

    [题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...

  7. codeforces 798B - Mike and strings

    感觉自己好咸鱼呀……B题写了这么久,虽然可以算作1A(忽略一次少include一个头文件的CE)…… 思想很简单,每次选定一个字符串作为目标字符串,然后把其他所有字符串都当做测试字符串,计算出总共需要 ...

  8. Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)

    传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...

  9. CodeForces 548A Mike and Fax (回文,水题)

    题意:给定一个字符串,问是不是恰好存在 k 个字符串是回文串,并且一样长. 析:没什么好说的,每次截取n/k个,判断是不是回文就好. 代码如下: #include<bits/stdc++.h&g ...

随机推荐

  1. 当年写的如何成为一名MSSQL DBA

    很多开发人员都想成为一名数据库培训,也有很多人一开始就把自己定位成为一名DBA,DBA究竟需要掌握些什么知识和技能呢?以下是我        做DBA工作和面试DBA时,整理的一些DBA方面的三十个问 ...

  2. 编译percona-server-locks-detail-5.7.22

    yum install -y binutils compat-libstdc++ gcc gcc-c++ glibc glibc-devel ksh libaio libaio-devel libgc ...

  3. python函数的用法

    python函数的用法 目录: 1.定义.使用函数 1.函数定义:def 2.函数调用:例:myprint() 3.函数可以当作一个值赋值给一个变量 例:a=myprint()    a() 4.写r ...

  4. python根据关键字以及id值爬取图虫网上高质量大图

    import requests import re import os import time print("ready....") def downPic(dirs, keywo ...

  5. Can We Make Operating Systems Reliable and Secure?

    Andrew S. Tanenbaum, Jorrit N. Herder, and Herbert Bos Vrije Universiteit, Amsterdam Microkernels-lo ...

  6. python数据类型练习题

    一.元素分类 有如下值集合 [11,22,33,44,55,66,77,88,99,90...],将所有大于 66 的值保存至字典的第一个key中,将小于 66 的值保存至第二个key的值中.即: { ...

  7. 04.Python网络爬虫之requests模块(1)

    引入 Requests 唯一的一个非转基因的 Python HTTP 库,人类可以安全享用. 警告:非专业使用其他 HTTP 库会导致危险的副作用,包括:安全缺陷症.冗余代码症.重新发明轮子症.啃文档 ...

  8. golang 开发gui

    可能因为我电脑上的mingw下只有gcc,没有g++的原因,之前用walk和andlabs都不成功 最后用github上gxui的sample代码终于编译出来一个丑陋的GUI,但编译过程也提示了一堆类 ...

  9. 【转】ffmpeg常用基本命令

    [FFmpeg]FFmpeg常用基本命令 1.分离视频音频流 ffmpeg -i input_file -vcodec copy -an output_file_video //分离视频流 ffmpe ...

  10. MySQL 主主配置

    一.准备 1.两个数据库版本最好保持一致(因为官方就是这么建议的,主要的问题就是考虑到兼容性问题) 2.连个数据库的数据保持一致,若不一致,可手动调整,比如A比B多一个库,那就将这个库导入到B库,达到 ...