A. Antipalindrome
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
A string is a palindrome if it reads the same from the left to the right and from the right to the left. For example, the strings "kek", "abacaba", "r" and "papicipap" are palindromes, while the strings "abb" and "iq" are not.
A substring s[l…r]s[l…r] (1 ≤ l ≤ r ≤ |s|1 ≤ l ≤ r ≤ |s|) of a string s = s1s2…s|s|s = s1s2…s|s| is the string slsl + 1…srslsl + 1…sr.
Anna does not like palindromes, so she makes her friends call her Ann. She also changes all the words she reads in a similar way. Namely, each word ss is changed into its longest substring that is not a palindrome. If all the substrings of ss are palindromes, she skips the word at all.
Some time ago Ann read the word ss. What is the word she changed it into?
Input
The first line contains a non-empty string ss with length at most 5050 characters, containing lowercase English letters only.
Output
If there is such a substring in ss that is not a palindrome, print the maximum length of such a substring. Otherwise print 00.
Note that there can be multiple longest substrings that are not palindromes, but their length is unique.
Examples
input
Copy
mew
output
Copy
3
input
Copy
wuffuw
output
Copy
5
input
Copy
qqqqqqqq
output
Copy
0
注意事项:
1.题意分析:
字符串 不是回文类型 如wert | 字符串 是回文类型 但字符不完全一样 如waaw | 字符串 是回文类型 但所有字符一样 如wwww |
输出 = 字符串长度 如4 | 输出 = 字符串长度-1 如3 | 输出 = 0 |
2.for(i = 0; i < strlen(a)/2; i++) 要折半 如果写出i < strlen(a) 答案就不对 具体原因我还没想清楚
#include<stdio.h>
#include<string.h>
int main()
{
char a[100],b[100];
int j = 0;
gets(a);
int i;
int result;
for (i = strlen(a) - 1; i >= 0; i--)
{
b[j++] = a[i];
}
b[j] = '\0';//自己添加结束符 因为赋值过程中不会添加的
if (strcmp(a,b) == 0)//是回文串
{
for(i = 0; i < strlen(a)/2; i++)//i<一半的长度
{
if(a[i]!=a[i+1])//是字符串不完全一样的回文串 如weew这种
{
result = strlen(a) -1;
break;
}
else
{
result = 0;//是字符串完全一样的回文串 如wwww这种
}
}
}
if (strcmp(a,b) != 0)
{
result = strlen(a);
}
printf("%d", result);
}