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

题目链接

时间复杂度 O(n*n*|s| )

纯暴力,通过string.substr()函数来构造每一个字符串平移后的字符串。

#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=;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
int n;
string getst(string x,int k,int len)
{
return x.substr(k,len-k)+x.substr(,k);
}
int main()
{
gg(n);
string a[];
repd(i,,n)
{
cin>>a[i];
}
int ans=inf;
string x=a[];
int len=x.length();
int flag=; repd(i,,n)
{
int cnt=;
string temp=a[i];
repd(j,,n)
{
int k;
for( k=;k<len;k++)
{
string xin=getst(a[j],k,len);
if(xin==temp)
{
cnt+=k;
break;
}
}
if(k==len)
{
flag=;
break;
}
}
if(flag)
{
break;
}
ans=min(ans,cnt);
}
if(flag)
{
printf("-1\n");
}else
{
printf("%d\n",ans );
}
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 - '';
}
}
}
上一篇:[EntLib]微软企业库5.0 学习之路——第一步、基本入门


下一篇:Luogu P1776 宝物筛选_NOI导刊2010提高(02)(多重背包模版)