NOJ [1251] Find the Palindrome
来源:互联网 发布:python 转置 优先级 编辑:程序博客网 时间:2024/05/30 05:15
[1251] Find the Palindrome
- The problem is pretty easy, giving you a very long string, you have to find the first longest palindrome in the string. (Case-insensitive)
- 输入
- First line contain a integer T (T <= 100), means the test case.
And the following T lines, for each line, there is a string is make up with the uppercase and lowercase.
The string's length is less than 10000. - 输出
- For each test case, you should print the longest palindrome, if there are muti-case, print first of them.
所以说,时间上差很多1.中心法代码#include<stdio.h>#include<string.h>char str[10010];int max_len,left,right,len;void search_longest_str(int l,int r){char a,b;a=str[l]; b=str[r]; if(a>='A' && a<='Z') a=a-'A'+'a'; if(b>='A' && b<='Z') b=b-'A'+'a';while(l>=0 && r<=len-1 && a==b){ l--;r++; a=str[l]; b=str[r]; if(a>='A' && a<='Z') a=a-'A'+'a'; if(b>='A' && b<='Z') b=b-'A'+'a'; }if(r-l+1>max_len){ max_len=r-l+1; left=l+1; right=r-1;} }int main(){int t;while(~scanf("%d",&t)){while(t--){scanf("%s",str);len=strlen(str);max_len=-1;for(int i=0;i<len;i++){search_longest_str(i,i);if(i+1<len) search_longest_str(i,i+1);}for(int i=left;i<=right;i++) printf("%c",str[i]); printf("\n");}}return 0;}
2.中间枚举i的代码#include<stdio.h>#include<string.h>char str[10010];char newstr[10010];int pos[10010];int main(){int t;scanf("%d",&t);while(t--){char a,b;int left,right,i,j,k,len;scanf("%s",str);len=strlen(str);int max_len=0;for(i=0;i<len;i++){if(str[i]>='A'&&str[i]<='Z') newstr[i]=str[i]-'A'+'a'; else newstr[i]=str[i]; pos[i]=i;}for(i=0;i<len;i++){for(j=0;i-j>=0 && i+j<len;j++){if(newstr[i-j]!=newstr[i+j]) break; if(2*j+1>max_len) { max_len=2*j+1; left=pos[i-j]; right=pos[i+j]; }}for(j=0;i-j>=0 && i+j+1<len;j++){ if(newstr[i-j]!=newstr[i+j+1]) break; if(2*j+2>max_len) { max_len=2*j+2; left=pos[i-j]; right=pos[i+j+1]; }}}for(i=left;i<=right;i++) printf("%c",str[i]); printf("\n");}return 0;}
0 0
- NOJ [1251] Find the Palindrome
- NOJ [1374] Find The Law
- NOJ--- Find the Prime Number Again(1155)
- 564. Find the Closest Palindrome
- 564. Find the Closest Palindrome
- NOJ [1148] Creating Palindrome
- hdu3068 Manacher --> find the longest palindrome
- 564. Find the Closest Palindrome (Hard)
- leetcode 564. Find the Closest Palindrome
- LeetCode 564: Find the Closest Palindrome(python)
- leetcode 564. Find the Closest Palindrome(java)
- [Leetcode] 564. Find the Closest Palindrome 解题报告
- the running man 【NOJ】
- [NOJ 1137] The Running Man
- NOJ--1137--The Running Man
- NOJ [1186] Get the Width
- NOJ [1306] Divide The Apples
- NOJ [1285] The TT Candy
- External sorting
- 初级数据持久化(本地存储)
- Linux环境编程之同步(二):条件变量
- Framework Manager入门教程(四) - 固定显示4个季度(FM建模)(中)
- 机器互联,远程登陆
- NOJ [1251] Find the Palindrome
- vi与终端进行切换
- JSP页面时间动态显示
- 当数组是函数的参数时, 为什么sizeof 不能正确报告数组的大小?
- 主宰这个世界的10种算法
- 通过SQL创建数据库、数据表
- 23种设计模式(4):建造者模式
- UE配置java环境的时候,输出窗口中文乱码
- Hyper-v 3.0上安装Linux集成服务