给定一些字符串,每次把对头的放在最后,问需要多少操作才能让他们相等
开始以为是贪心,或者感觉无处可贪,应该是暴力。
题解中大概有三个版本。
第一个最简单的,因为运用了stl中的find函数,所以比较简单了
因为要涉及循环比较,所以每个串都加了一倍的长度。
然后直接find就好。
两个代码的整体实现方法都一样,但是用的手段不一样。
一个是用的stl(棒棒哒)
另一个是用的取模的思想和一个小技巧(66哒)
http://codeforces.com/contest/798/my
#include <iostream>
#include <cstdio>
#include <bits/stdc++.h>
const int maxn=200;
using namespace std;
string m[maxn];
int main()
{ std::ios::sync_with_stdio(false);
cin.tie(0);
int n;
cin>>n;
for(int i=1;i<=n;i++)
cin>>m[i];
string tmp;
int cnt;
int sum=0x3f3f3f3f;
for(int i=1;i<=n;i++)
{ cnt=0;//
for(int j=1;j<=n;j++)
{ tmp=m[j]+m[j];
if(tmp.find(m[i])==string::npos)
{ cout<<"-1"<<endl;
return 0;
}
cnt+=tmp.find(m[i]);
}
sum=min(cnt,sum);
}
cout<<sum<<endl;
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e9;
const int MAXN = 100;
string s[MAXN];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for(int i=0;i<n;i++)
cin>>s[i];
int m = s[0].length();
int ans = INF;
for(int k=0;k<m;k++)
{
int t = 0;
for(int i=0;i<n;i++)
{
bool good = false;
for(int j=0;j<m;j++)
{
bool ok = true;
for(int g=0;g<m;g++)
{
ok &= (s[0][(k+g)%m] == s[i][(j+g)%m]);
}
if(ok)
{
good = true;
t += j;
break;
}
}
if(!good)
{
cout << -1 << endl;
return 0;
}
}
ans=min(ans,t);
}
cout << ans << endl;
return 0;
}