2019CCPC网络赛——1003——K-th occurrence

 

K-th occurrence

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 0    Accepted Submission(s): 0

 

Problem Description

You are given a string S consisting of only lowercase english letters and some queries.

For each query (l,r,k) , please output the starting position of the k-th occurence of the substring SlSl+1...Sr in S.

 

 

Input

The first line contains an integer T(1≤T≤20) , denoting the number of test cases.

The first line of each test case contains two integer N(1≤N≤105),Q(1≤Q≤105) , denoting the length of S and the number of queries.

The second line of each test case contains a string S(|S|=N) consisting of only lowercase english letters.

Then Q lines follow, each line contains three integer l,r(1≤l≤r≤N) and k(1≤k≤N) , denoting a query.

There are at most 5 testcases which N is greater than 103 .

 

 

Output

For each query, output the starting position of the k-th occurence of the given substring.

If such position don't exists, output −1 instead.

 

 

Sample Input

 

2

12 6

aaabaabaaaab

3 3 4

2 3 2

7 8 3

3 4 2

1 4 2

8 12 1

1 1

a

1 1 1

 

 

Sample Output

 

5

2

-1

6

9

8

1

 

没有过的两个代码。。。留着以后改改吧(虽然不一定会动)

#include <iostream>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>

using namespace std;

int main(void)
{
	int t;
	scanf("%d",&t);
	while(t--){
		int n,q;
		string s,ss;
		ss = "";
		scanf("%d %d",&n,&q);
		char a;
		for(int i=1; i<=n; i++){
			cin >> a;
			s += a;
		}
		cout << s << endl;
		
		
		for(int qq=0; qq<q; qq++){//q次询问 
			
			ss = "";//清空子串 
			int l,r,k;
			scanf("%d %d %d",&l,&r,&k);
		
			for(int i=l-1; i<r; i++){
				ss += s[i];
			}//子串
			
			
			cout << ss << endl;
			
			int sum = 0;
			while(s.find(ss) != -1){
				int pos = s.find(ss);
				sum++;
				
				s.erase(0,pos+1);
				
				if(sum == k){
					cout << pos << endl;
				}
			}
		
			/*int sum = 0;//次数
			int i;
			for(i=0; i<n; i++){
				int flag = 0;
				for(int j=0; j<ss.length(); j++){
					if(s[i] != ss[j]){
						flag = 1;//不是子串 
					}
				}
			
				if(flag == 0){//是字串 
					sum++; 
					cout << sum << endl;
				}
			
				if(sum == k){
					printf("%d\n",i+1);
					break;
				}
			}*/
			
		}		
		
	}
	
	return 0;
}

 

#include <iostream>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>

using namespace std;

int main(void)
{
    int t;
    scanf("%d",&t);
    while(t--){
        int n,q;
        scanf("%d %d",&n,&q);
        string s = "";
        for(int i=0; i<n; i++){//输入原串 
            char a;
            cin >> a;
            s += a;
        }
        
        while(q--){
            int l,r,k;
            scanf("%d %d %d",&l,&r,&k);
            
            string temp = s;
            string ss = "";
            for(int i=l-1; i<r; i++){
                ss += temp[i];
            }
            
            int sum = 0;//次数
            int i;//位置
            
            int pos = temp.find(ss) + 1;
            if(pos == 0){
                cout << "-1" << endl;
            }
            else{
                int ff = 0;
                for(int i=0; i<temp.length(); i++){
                    if(s[i] != s[0]){
                        ff = 1;
                    }
                }
                
                if(ff == 0){
                    while(!temp.empty()){
                        int pos = temp.find(ss) + 1;//第一次出现的位置 
                        sum++;
                        if(pos == 0){//没有找到这个字符串 
                            sum--;
                        }
                    
                        if(sum == k){
                            int x = temp.length() - pos;
                            int index = n - x;
                            cout << index << endl;
                            break;
                        }
                        temp.erase(0,1);

                    }
                }else if(ff == 1){
                    while(!temp.empty()){
                        int pos = temp.find(ss) + 1;//第一次出现的位置 
                        sum++;
                        if(pos == 0){//没有找到这个字符串 
                            sum--;
                        }
                    
                        if(sum == k){
                            int x = temp.length() - pos;
                            int index = n - x;
                            cout << index << endl;
                            break;
                        }
                        temp.erase(0,pos+ss.length()-1);

                    }
                }
                
            }
        
            
             
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值