Submission #1439654


Source Code Expand

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<numeric>

#define rep(n) for(int i=0;i<n;i++)
#define repp(j, n) for(int j=0;j<n;j++)
#define reppp(i, m, n) for(int i=m;i<=n;i++)
#define all(c) c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define pb(x) push_back(x)
#define eb(x,y) emplace_back(x,y)
#define MOD 1000000007
#define MAX 1000000001
#define INF 1410065408
#define EPS 1e-9
 
using namespace std;
typedef long long ll;
typedef pair<ll, ll> Pll;
typedef pair<int, int> Pii;
struct edge{int from, to; ll cost;};

signed main(){
    int n, k;
    cin >> n >> k;
    if(n == k) return 0;
    vector<int> a(k);
    rep(k){
        cin >> a[i];
        a[i]--;
    }

    string s, longestKeyword;
    vector<string> rest;
    int ai = 0;
    rep(n){
        cin >> s;
        if(ai < k && i == a[ai]){
            if(ai == 0){
                longestKeyword = s;
            }else{
                int newLength = 0;
                while(newLength < min((int)s.length(), (int)longestKeyword.length())){
                    if(s[newLength] == longestKeyword[newLength]) newLength++;
                    else break;
                }
                if(newLength < (int)longestKeyword.length()) longestKeyword = longestKeyword = longestKeyword.substr(0, newLength);
            }
            ai++;
        }else{
            rest.pb(s);
        }
    }
    if(longestKeyword.empty()){
        cout << "-1";
        return 0;
    }

    int minLength = 0;
    for(string r : rest){
        int newMinLength = 0;
        while(newMinLength < min((int)r.length(), (int)longestKeyword.length())){
            if(r[newMinLength] == longestKeyword[newMinLength]) newMinLength++;
            else break;
        }
        if(newMinLength + 1 > minLength) minLength = newMinLength + 1;
    }
    if(minLength > (int)longestKeyword.length()){
        cout << "-1";
    }else{
        cout << longestKeyword.substr(0, minLength);
    }
}

Submission Info

Submission Time
Task C - 検索
User Noimin
Language C++14 (GCC 5.4.1)
Score 0
Code Size 2172 Byte
Status WA
Exec Time 42 ms
Memory 5748 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 400
Status
AC × 3
AC × 16
WA × 14
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All sample1.txt, sample2.txt, sample3.txt, in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in3.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, long.txt, long2.txt, sample1.txt, sample2.txt, sample3.txt, long.txt, long2.txt
Case Name Status Exec Time Memory
in1.txt WA 5 ms 384 KB
in10.txt WA 3 ms 384 KB
in11.txt WA 9 ms 896 KB
in12.txt WA 4 ms 384 KB
in13.txt WA 5 ms 512 KB
in14.txt WA 5 ms 384 KB
in15.txt AC 3 ms 384 KB
in16.txt AC 1 ms 256 KB
in17.txt WA 4 ms 384 KB
in18.txt WA 5 ms 384 KB
in19.txt WA 9 ms 896 KB
in2.txt WA 10 ms 896 KB
in20.txt WA 5 ms 512 KB
in3.txt AC 42 ms 5748 KB
in4.txt AC 1 ms 256 KB
in5.txt WA 5 ms 384 KB
in6.txt AC 2 ms 256 KB
in7.txt WA 4 ms 384 KB
in8.txt WA 8 ms 896 KB
in9.txt AC 1 ms 256 KB
long.txt AC 4 ms 512 KB
long2.txt AC 16 ms 2428 KB
sample1.txt AC 1 ms 256 KB
sample2.txt AC 1 ms 256 KB
sample3.txt AC 1 ms 256 KB