A. Square String?

A. Square String?
time limit per test:1 second
memory limit per test:256 megabytes
input:standard input
output:standard output

A string is called square if it is some string written twice in a row. For example, the strings "aa", "abcabc", "abab" and "baabaa" are square. But the strings "aaa", "abaaab" and "abcdabc" are not square.

For a given string s determine if it is square.

Input

The first line of input data contains an integer t (1t100) —the number of test cases.

This is followed by t lines, each containing a description of one test case. The given strings consist only of lowercase Latin letters and have lengths between 1 and 100 inclusive.

Output

For each test case, output on a separate line:

  • YES if the string in the corresponding test case is square,
  • NO otherwise.

You can output YES and NO in any case (for example, strings yEsyesYes and YES will be recognized as a positive response).

Example
input
10
a
aa
aaa
aaaa
abab
abcabc
abacaba
xxyy
xyyx
xyxy
output
NO
YES
NO
YES
YES
YES
NO
NO
NO
YES


思路:这个其实是在判断一个字符串是否由两个“相同”的字符串相加得到的。这里就有一个关键点了,如果这个字符串长度不是偶数,那就必然不是。如果是偶数,那就把平均分开,也就是双指针,一个指向第一个字符,另个指向中间的那个字符,然后同时后移,如果遇到有不相等的情况就不是,如果直到结束都是相等的,那么就输出YES。

题解:

#include<stdio.h>
#include<string.h>
char s[100005]; //存储字符串 
int main(){
	int n; //字符串个数
	scanf("%d",&n);
	while(n--){  //小技巧:用while的方式来减少空间复杂度!! 
		int cnt=0;  //计算字符串前后字符达到重复的个数 
		scanf("%s",s);
		int len = strlen(s); //计算字符串长度,后期用来算中间索引 
		for(int i=0;i<(len+1)/2;i++){
			if(s[i]==s[(len+1)/2+i]){
				cnt++;
			}
		}
		if(cnt==(len+1)/2){
			printf("YES\n");
		}else{
			printf("NO\n");
		}
	} 
	return 0;
} 

//知识点:用char的方式存储字符串,就可以很好处理字符串! 

 

posted @ 2022-01-18 13:43  霜鱼CC  阅读(13)  评论(0)    收藏  举报