Palindromes
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 1992 Accepted Submission(s): 946
Problem Description
A regular palindrome is a string of numbers or letters that is the same forward as backward. For example, the string “ABCDEDCBA” is a palindrome because it is the same when the string is read from left to right as when the string is read from right to left.
Now give you a string S, you should count how many palindromes in any consecutive substring of S.
Input
There are several test cases in the input. Each case contains a non-empty string which has no more than 5000 characters.
Proceed to the end of file.
Output
A single line with the number of palindrome substrings for each case.
Sample Input
aba
aa
Sample Output
4
3
一个回文问题,当时第一个想法就是先写一个判断回文的函数,然后再写一个遍历字符串的一个函数,接着主函数输出就行
第一次代码(WA)
#include <iostream>
#include <cstring>
using namespace std;
int ans;
int f(char []);
char function(char []);
int main(void)
{
char str[20];
cin>>str;
if (f(*function(str)) == 1)
{
ans ++;
}
printf("%d",ans);
return 0;
}
int f(char str[])
{
int len = strlen(str);
for(int i = 0, j = len-1; i <= len/2; i ++, j --)
{
if(str[i] != str[j])
return 0;
}
return 1;
}
char function(char str[])
{
int len = strlen(str);
char a[20];
int p;
p = 0;
for(int i = 0; i < len; i ++)
{
for(int j = i; j < len; j ++)
{
a[p] = str[j];
p ++;
}
}
return *a;
}
不太会使用数组指针,为了解决这个问题,引入了全局变量
第二次代码(WA)
#include <iostream>
#include <cstring>
using namespace std;
int ans;
int f(char []);
char function(char []);
int main(void)
{
char str[20];
cin>>str;
if (f(*function(str)) == 1)
{
ans ++;
}
printf("%d",ans);
return 0;
}
int f(char str[])
{
int len = strlen(str);
for(int i = 0, j = len-1; i <= len/2; i ++, j --)
{
if(str[i] != str[j])
return 0;
}
return 1;
}
char function(char str[])
{
int len = strlen(str);
char a[20];
int p;
p = 0;
for(int i = 0; i < len; i ++)
{
for(int j = i; j < len; j ++)
{
a[p] = str[j];
p ++;
}
}
return *a;
}
极其弱智的问题,function函数只能返回一次数组,相当于遍历子串的时候,如果输入的是ABC的情况,只考虑了ABC,没有考虑A、B、C、AB、BC
第三次代码(WA)
#include <iostream>
#include <cstring>
using namespace std;
int ans = 0;
char a[20];
int p = 0;
int f(char []);
void function(char []);
int main(void)
{
char str[20];
cin>>str;
function(str);
int u = strlen(str);
printf("%d",ans + u);
return 0;
}
int f(char str[])
{
int len = strlen(str);
for(int i = 0, j = len-1; i <= len/2; i ++, j --)
{
if(str[i] != str[j])
return 0;
}
return 1;
}
void function(char str[])
{
int len = strlen(str);
for(int i = 0; i < len; i ++)
{
for(int j = i; j < len; j ++)
{
a[p] = str[j];
p ++;
}
if(f(a) == 1)
{
ans ++;
}
p = 0;
}
}
当时没想多少,直接把f函数放到function函数里了,结果不太对,后来发现没有把p给刷新,就是说在function函数的第二层循环里,经历过一次循环后,p的值等于len - 1,但是下一次进入第二层循环时,p是从0开始的,所以加了一个p = 0;
第四次代码(WA)
#include <iostream>
#include <cstring>
using namespace std;
int ans = 0;
char a[20];
int p = 0;
int f(char []);
void function(char []);
int main(void)
{
char str[20];
while (~scanf("%s", str))
{
function(str);
int u = 0;
u = strlen(str);
printf("%d",ans + u);
return 0;
}
}
int f(char str[])
{
int len = strlen(str);
for(int i = 0, j = len-1; i <= len/2; i ++, j --)
{
if(str[i] != str[j])
return 0;
}
return 1;
}
void function(char str[])
{
int len = strlen(str);
for(int i = 0; i < len; i ++)
{
for(int j = i + 1; j < len; j ++)
{
a[p] = str[j];
if(f(a) == 1)
{
ans ++;
}
p ++;
}
for (int q = 0; q < p; q ++)
{
a[p] = '0';
}
p = 0;
}
}
第三次代码调试后依然不对,想了很长时间,才发现不仅p要刷新,a[p]也要刷新,因为f函数是对整个a数组进行处理,举个例子,如果判断的字符串是ABC,那么function函数的第二层循环的第一次循环之后a[0]=A,然后判断a[0]是不是回文,之后a[1] = B,然后判断a[0] - a[1]的这一部分,是不是回文,依此类推,最后a[0]=A,a[1]=B,a[2]=C;这时候再想如果第二层循环开始了第二次循环,那么按照上面的想法可以知道,a[0]被赋值为B,然后进入判断语句,之后问题就来了,上一次的循环a[1] ,a[2]都是有值的,而判断语句是判断整个a[]数组的,所以存在问题,需要把a数组刷新一下.
是不是以为结束了,嘿嘿,并没有,我dnmd时间复杂度,细心的人估计可以发现我在使用函数里面再套一个函数的时候三重循环了,所以什么刷新p,刷新a[p]之类的,都是扯淡,一个时间复杂度直接让我原地爆炸
换思路
第五次代码(AC)
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
char s[5005];
int main()
{
int m,n;
while (~scanf("%s", s))
{
int len;
len = strlen(s);
int ans;
ans = 0;
for (int i = 0; i < len; i ++)
{
m = i - 1;
n = i + 1;
while (m >= 0 && n < len && s[m] == s[n])
{
ans ++;
m --;
n ++;
}
m = i;
n = i + 1;
while (m >= 0 && n < len && s[m] == s[n])
{
ans ++;
m --;
n ++;
}
}
printf("%d\n", ans + len);
}
return 0;
}
代码很简单,主要是不太好想,简单看看就明白了.
时间复杂度yyds,毁灭吧,我累了