题目链接

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

思路:

既然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. socket网络编程之不间断通信

    socket是python提供的一种网络通信方式. socket是应用层与TCP/IP协议通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议 ...

  2. c/c++ 图的创建及图的相关函数(链表法)

    c/c++ 图的创建及图的相关函数(链表法) 图的概念 图由点和线组成 知道了图中有多少个点,和哪些点之间有线,就可以把一张图描绘出来 点之间的线,分有方向和无方向 创建图 创建图,实际就是创建出节点 ...

  3. php curl参数详解之post方法

    利用记录的URL参数解释,写一个post方法: <?php function do_post($url, $data) { $ch = curl_init(); //设置CURLOPT_RETU ...

  4. Vue学习之路4-v-bind指令

    1. 定义 1.1 v-bind 指令被用来响应地更新 HTML 属性,其实它是支持一个单一 JavaScript 表达式 (v-for 除外). 2. 语法 2.1 完整语法:<span v- ...

  5. LeetCode算法题-Climbing Stairs(Java实现)

    这是悦乐书的第159次更新,第161篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第18题(顺位题号是70).你正在爬楼梯,它需要n步才能达到顶峰.每次你可以爬1或2步, ...

  6. C#字节数组与字符串转换

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  7. php面试题整理(五)

    表单也得改

  8. 【css】常用css

    常用css--------下三角 常用css--------闪动效果 css #shandongFlash { width:100px; height:100px; background:#f8b55 ...

  9. Sphinx 生成 Windows 帮助文件 (.chm文件)

    本文不介绍 Sphinx 的用法,只简要罗列 Windows 下生成 .chm 文件的步骤. 0. 首先检查机器是否安装了 HTML Help Workshop 软件,一般安装路径应该是 C:\Pro ...

  10. Emacs 中 GDB 的使用

    Emacs 提供了方便的 GDB 调试功能,使用方法简明如下, 1. 编译时加入调试信息, 例如: $ clang++ -g -Wall t.cpp -o t 2. 调试 t ,直接用命令 M-x g ...