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 ...
随机推荐
- sqlserver waitfor time 延迟函数的用法
SQL有定时执行的语句 WaitFor,可以写到一个存储过程中再执行一次 语法:WaitFor{Delay 'time'|Time 'time} Delay后面的时间为延迟多少时间执行 Time后面的 ...
- PHP中生产不重复随机数的方法
PHP内置函数不重复随机数 需求:要生成一个数组,这个数组里面有10个元素,都是整形,并且是1-60之间不重复的随机数. 代码: 代码示例: 1 2 3 4 5 6 7 8 9 10 ...
- ctf学习(web题二)
web 下面是做bugku上一些web的总结 内容链接
- 17秋 软件工程 团队第五次作业 Alpha
题目:团队作业--Alpha冲刺 17秋 软件工程 团队第五次作业 Alpha 12次Scrum 第一次Scrum 第二次Scrum 第三次Scrum 第四次Scrum 第五次Scrum 第六次Scr ...
- WPF自定义控件(五)の用户控件(完结)
用户控件,WPF中是继承自UserControl的控件,我们可以在里面融合我们的业务逻辑. 示例:(一个厌恶选择的用户控件) 后端: using iMicClassBase; using iMicCl ...
- UVA12188-Inspector's Dilemma(欧拉回路+连通性判断)
Problem UVA12188-Inspector's Dilemma Time Limit: 3000 mSec Problem Description In a country, there a ...
- BeautifulSoup类
from bs4 import BeautifulSoup soup1 = BeautifulSoup("<html>data</html>"," ...
- P1515 旅行(简单搜索)
非常简单的搜索. 思路:先排序,然后,搜索枚举的时候满足A < 两个旅店 < B,然后,搜索就行了. #include<iostream> #include<algori ...
- Raid卡介绍
raid0条带卷 最少需要一块硬盘 可以把所有硬盘的容量都叠加在一起,可以拥有很高的读写速度,硬盘空间也能得到很好的利用 但是只要其中一块硬盘换了,数据就全丢失了 raid1镜像卷 最少需要两块硬盘, ...
- python3 迭代器
''' 迭代:重复的过程,并且每次迭代的结果都是下次迭代的初始值 可迭代的 Iterable 迭代器 Iterator iter(数据) == 数据.__iter__() 将数据转换为迭代器 next ...