题目链接: 点这里
题意:略。
思路:按照每个字符串的长度排个序,然后对每个串二分查找长度相同的串。
AC代码:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <cmath>
#include <map>
#include <set>
#define ll long long
#define llu unsigned long long
#define INF 0x3f3f3f
using namespace std;
const int maxn = 1011130;
const int N = 111123;
const double eps = 1e-10;
const double PI = acos(-1.0);
int n;
struct node {
char s[20];
int len;
}p[55000];
bool cmp(node a,node b) {
return a.len < b.len;
}
int lound(int x) {
int l = 1,r = n+1;
while(l<r) {
int mid = (l+r)>>1;
if(p[mid].len >= x) {
r = mid;
}else {
l = mid+1;
}
}
return (l+r)/2;
}
int main(){
// ios::sync_with_stdio(false);
scanf("%d",&n);
char str[20];
for(int i=1;i<=n;i++) {
scanf("%s",str);
strcpy(p[i].s,str);
p[i].len = strlen(str);
}
sort(p+1,p+n+1,cmp);
int ans = 0;
for(int i=1;i<n; i++) {
int l = p[i].len;
int ix = lound(l);
int iy = lound(l+1);
// cout<<"ix="<<ix<<" iy="<<iy<<endl;
for(int j=ix; j<iy; j++) {
int idx = 0;
int f = 0;
while(idx < l) {
if(p[i].s[idx] != p[j].s[l-idx-1]) {
f = 1; break;
}
idx++;
}
if(f == 0) {
ans++; break;
}
}
// cout<<"ans="<<ans<<endl;
}
printf("%d\n",(ans+1)/2);
return 0;
}
/*
7
dog
live
hiho
evil
coder
god
redoc
*/