Mike and strings CodeForces - 798B (又水又坑)
题意:英语很简单,自己取读吧。
思路:
既然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 (又水又坑)的更多相关文章
- Mike and strings CodeForces - 798B (简洁写法)
题目链接 时间复杂度 O(n*n*|s| ) 纯暴力,通过string.substr()函数来构造每一个字符串平移后的字符串. #include <iostream> #include & ...
- Mike and strings 798B
B. Mike and strings time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- 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 ...
- CF410div2 B. Mike and strings
/* CF410div2 B. Mike and strings http://codeforces.com/contest/798/problem/B 字符串 暴力 题意:给你n个串,每次操作可以将 ...
- 牛客OI周赛8-提高组A-用水填坑
牛客OI周赛8-提高组A-用水填坑 题目 链接: https://ac.nowcoder.com/acm/contest/403/A 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制: ...
- 【codeforces 798B】Mike and strings
[题目链接]:http://codeforces.com/contest/798/problem/B [题意] 给你n个字符串; 每次操作,你可以把字符串的每个元素整体左移(最左边那个字符跑到最后面去 ...
- codeforces 798B - Mike and strings
感觉自己好咸鱼呀……B题写了这么久,虽然可以算作1A(忽略一次少include一个头文件的CE)…… 思想很简单,每次选定一个字符串作为目标字符串,然后把其他所有字符串都当做测试字符串,计算出总共需要 ...
- Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
传送门 Description Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In ...
- CodeForces 548A Mike and Fax (回文,水题)
题意:给定一个字符串,问是不是恰好存在 k 个字符串是回文串,并且一样长. 析:没什么好说的,每次截取n/k个,判断是不是回文就好. 代码如下: #include<bits/stdc++.h&g ...
随机推荐
- [Hive_5] Hive 的 JDBC 编程
0. 说明 Hive 的 JDBC 编程 1. hiveserver2 介绍 hiveserver2 是 Hive 的 JDBC 接口,用户可以连接此端口来连接 Hive 服务器 JDBC 驱动类为 ...
- nginx 拦截 swagger 登录
随着微服务的也来越多,每个服务都有单独的文档,那么问题来了,怎么把所有文档整合在一起呢 本方法采用服务器拦截的方式进行处理 首先需要在opt 的主目录中 /opt/ 创建一个新文件 htpasswd此 ...
- Windows10反安装报错error code 2502 2503
先找系统TEMP目录,一般为C:\windows\temp,打开这个目录的权限,为这个目录中的User用户添加权限为完全控制,现在再反安装就不会报错了. 注:原因就是因为系统运行时需要用到临时文件的目 ...
- 小程序背景图片bug
在pc端调试的时候已经可以看到出现背景图片了,但是在真机调试的时候却发现没有背景图片,那么原因是什么呢?真机调试和vconsole也看不出什么鸟,其实这是小程序的一个bug.另一种说法是:backgr ...
- A. On The Way to Lucky Plaza 概率 乘法逆元
A. On The Way to Lucky Plaza time limit per test 1.0 s memory limit per test 256 MB input standard i ...
- dp P1103 书本整理 洛谷
题目描述 Frank是一个非常喜爱整洁的人.他有一大堆书和一个书架,想要把书放在书架上.书架可以放下所有的书,所以Frank首先将书按高度顺序排列在书架上.但是Frank发现,由于很多书的宽度不同,所 ...
- 2.01-request_header
import urllib.request def load_baidu(): url= "https://www.baidu.com" header = { #浏览器的版本 &q ...
- java中的闭包
闭包(Closure)是一种能被调用的对象,它保存了创建它的作用域的信息 public class Programmer { private String name; public Programme ...
- Oracle hint之ORDERED和USE_NL
Hint:ORDERED和USE_NL ORDERED好理解,就是表示根据 from 后面表的顺序join,从左到右,左边的表做驱动表 use_nl(t1,t2):表示对表t1.t2关联时采用嵌套循环 ...
- 【转】iOS弹幕库OCBarrage-如何hold住每秒5000条巨量弹幕
最近公司做新需求, 原来用的老弹幕库, 已经无法满足需要. 迫不得已自己写了一套弹幕库OCBarrage. 这套弹幕库轻量, 可拓展, 高度自定义, 超高性能, 简单易上手. 无论哪家公司软件的性能绝 ...