2519: Find the longest section 位运算+前缀和

本文介绍了一种算法,用于解决寻找多个字符串中最长连续子串的问题,其中子串的所有字符出现次数均为偶数。该算法通过位运算来快速判断字符串中字符的奇偶性,并利用排序技巧找到符合条件的最长子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2519: Find the longest section


There are n strings with the length of no more than 100 characters. Can you find the longest continuous string section(ie. S(i)S(i+1)...S(j) and j-i+1 is maximaze) that all the letters appear even times.

Input

There will be multiple test case. For each test: The first line has a positive number n(1 <= n <= 10000). Then followed n rows, each row has a string making up with lowercase letters.

Output

Output the longest section(left, right),where left is the beginning loction of the section and right is the ending loction of the section. If there is more than one such section, output the one whose left is smallest.If there is none such section,just output "No one".

Sample Input

3
a
ab
b
7
a
ab
b
e
wang
wang
wangwang
2
mars
wang

Sample Output

1 3
1 3
No one
 
 
 
 
 
 
 

 

//

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
const int maxn=10000+100;
char str[300];
struct Node
{
 int sum;
 int index;
};
Node g[maxn];
bool cmp(Node h,Node k)
{
 if(h.sum!=k.sum) return h.sum<k.sum;
 return h.index<k.index;
}
int main()
{
 int n;
 while(scanf("%d",&n)==1)
 {
  for(int i=0;i<=n;i++) g[i].sum=0,g[i].index=i;
  for(int i=1;i<=n;i++)
  {
   scanf("%s",str);
   int cnt=0;
   for(int j=0;str[j];j++)
   {
    cnt=cnt^(1<<(str[j]-'a'));
   }
   g[i].sum=g[i-1].sum^cnt;
  }
  sort(g,g+n+1,cmp);
  int flag=0,st=-1,ed=-1,_max=0;
  for(int i=0;i<n;)
  {
   if(g[i].sum==g[i+1].sum)
   {
    flag=1;
    int j=i+1;
    while(j<=n&&g[i].sum==g[j].sum) j++;
    int len=g[j-1].index-g[i].index;
    if(len>_max) _max=len,st=g[i].index,ed=g[j-1].index;
    else if(len==_max)
    {
     if(st>g[i].index) st=g[i].index,ed=g[j-1].index;
    }
    i=j;
   }
   else
   {
    i++;
   }
  }
  if(!flag) printf("No one/n");
  else printf("%d %d/n",st+1,ed);
 }
 return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值