生成随机数据
data.cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn=1e4+10;
int main()
{
ll a,b;
srand(time(0));
ll n;
n=rand()%20+2;
ll m=rand()%20+2;
string s[maxn];
for(ll i=1;i<=n;i++){
for(ll j=1;j<=m;j++){
s[i]+=char('a'+rand()%26);
}
}
cout<<n<<' '<<m<<endl;
for(ll i=1;i<=n;i++){
cout<<s[i]<<endl;
}
return 0;
}
解决代码
solve.cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const ll maxn=1e4+10;
string s[maxn];
ll n,m;
ll cal(string x,string y){
ll i;
for(i=0;i<m;i++){
if(x[i]!=y[i]){
break;
}
}
return i+1;
}
void solve(){
ll ans=0;
ll t=1,len,nl,i;
ll tem=0;
while(1){
ll flag=0;
for(i=t+1;i<=n;i++){
len=cal(s[t],s[i]);
// cout<<t<<' '<<i<<endl;
// cout<<"len"<<len<<endl;
if(flag==0){
nl=len;
flag=1;
}else {
if(len<nl){
// cout<<"nl"<<nl<<endl;
ans+=(i-t)*nl;
// cout<<ans<<endl;
t=i;
break;
}
}
}
if(t==n){
ans+=len;
break;
}
if(i==n+1){
ans+=(i-t)*nl;
break;
}
}
cout<<ans<<endl;
return ;
}
int main()
{
cin>>n>>m;
for(ll i=1;i<=n;i++){
cin>>s[i];
}
sort(s+1,s+1+n);
solve();
return 0;
}
标准代码
std.cpp
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e4+10;
int dist[maxn];
string s[maxn];
int main()
{
int n,l,sum=0,i,j;
cin>>n>>l;
for(i=1;i<=n;i++){
cin>>s[i];
}
sort(s+1,s+1+n);
for(i=1;i<n;i++){
for(j=0;j<l;j++){
if(s[i][j]!=s[i+1][j]){
dist[i]=j+1;
break;
}
}
}
dist[0]=dist[1];
dist[n]=dist[n-1];
// for(i=1;i<=n;i++){
// cout<<dist[i]<<' ';
// }cout<<endl;
for(i=1;i<=n;i++){
sum+=max(dist[i],dist[i-1]);
}
cout<<sum<<endl;
return 0;
}
对拍代码
duipai.cpp
#include<bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
ll t=0;
while(1){
cout<<"test "<<(++t)<<endl;
system("data.exe>data.in");
system("solve.exe<data.in>solve.out");
system("std.exe<data.in>std.out");
if(system("fc std.out std.out >diff.log")){
cout<<"WA"<<endl;
break;
}
cout<<"AC"<<endl;
}
return 0;
}