Codeforces Round #644 (Div. 3) 参赛人数20688
[codeforces 1360F] Spy-string 难想的爆搜
总目录详见https://blog.csdn.net/mrcrack/article/details/103564004
在线测评地址https://codeforces.com/contest/1360/problem/F
Problem | Lang | Verdict | Time | Memory |
---|---|---|---|---|
F - Spy-string | GNU C++17 | Accepted | 31 ms | 0 KB |
判定是否符合题意的时间复杂度O(n*m)
每个位置的字母,用26个字母进行替换,并进行判定的时间复杂度O(m*26*n*m),数据有t组,总的时间复杂度O(t*m*26*n*m)
t=100,n=10,m=10,100*10*26*10*10=2.6*10^6故,爆搜不会超时。
样例模拟如下
2 4
abac
zbab
位置 0 1 2 3
字串1的字母a b a c
字串2的字母z b a b
字串1的0位置上字母,自'a'变化到'z'过程中,
发现
zbac与字串1(abac)只差一位字母
zbac与字串2(zbab)只差一位字母
故zbac是符合题意的字串。
AC代码如下
#include <stdio.h>
#include <string.h>
char s[15][15],ans[15];
int n,m;
int judge(char *a){//判定Formally, for each given string ai, there is no more than one position j such that ai[j]≠s[j].
int i,j,cnt;
for(i=0;i<n;i++){
cnt=0;//统计不同字母的数量
for(j=0;j<m;j++)
if(s[i][j]!=a[j])cnt++;
if(cnt>1)return 0;
}
return 1;
}
int main(){
int t,i,j;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)scanf("%s",s[i]);
strcpy(ans,s[0]);
if(judge(ans))printf("%s\n",ans);
else{
int flag=0;
for(i=0;i<m;i++){//i表示字母需要改动的位置
strcpy(ans,s[0]);
for(j=0;j<26;j++){//26个字母
ans[i]='a'+j;
if(judge(ans)){flag=1,printf("%s\n",ans);break;}
}
if(flag)break;
}
if(!flag)printf("-1\n");
}
}
return 0;
}