import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner;

import java.io.IOException;
import java.util.StringTokenizer;

// >>> Don't Change
public class OrphanPages extends Configured implements Tool {

    public static void main(String[] args) throws Exception {
        int res = ToolRunner.run(new Configuration(), new OrphanPages(), args);
        System.exit(res);
    }
// <<< Don't Change

    @Override
    public int run(String[] args) throws Exception {
        //TODO
		Job job = Job.getInstance(this.getConf(), "Orphan Pages");
		job.setOutputKeyClass(Integer.class);
		job.setOutputValueClass(NullWritable.class);

		job.setMapOutputKeyClass(IntWritable.class);
		job.setMapOutputValueClass(IntWritable.class);

		job.setMapperClass(LinkCountMap.class);
		job.setReducerClass(OrphanPageReduce.class);

		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		job.setJarByClass(OrphanPages.class);
		return job.waitForCompletion(true) ? 0 : 1;
    }

    public static class LinkCountMap extends Mapper<Object, Text, IntWritable, IntWritable> {
        @Override
        public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
            //TODO
			String[] referralAndPages = value.toString().split(":");
			if (referralAndPages.length >= 1){
				context.write(new IntWritable(Integer.valueOf(referralAndPages[0].trim())), new IntWritable(0));
			}
			if (referralAndPages.length == 2){
				String[] referredPages = referralAndPages[1].split(" ");
				for(String referredPage: referredPages){
					context.write(new IntWritable(Integer.valueOf(referredPage.trim())), new IntWritable(1));
				}
			}
		}
    }

    public static class OrphanPageReduce extends Reducer<IntWritable, IntWritable, IntWritable, NullWritable> {
        @Override
        public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
            //TODO
			int refLinksNum = 0;
			for(IntWritable page: values){
				refLinksNum += page.get();
			}
			if (refLinksNum == 0){
				context.write(key, NullWritable.get());
			}
        }
    }
}
@@ -1822,28 +1822,17 @@
 tring%5B%5D 
-referralAndP
+p
 ages = v
@@ -1862,36 +1862,25 @@
 :%22);%0A%09%09%09if (
-referralAndP
+p
 ages.length 
@@ -1894,38 +1894,23 @@
 %09%09%09%09
-context.write(new IntWritable(
+int referral = 
 Inte
@@ -1921,28 +1921,17 @@
 valueOf(
-referralAndP
+p
 ages%5B0%5D.
@@ -1929,32 +1929,76 @@
 pages%5B0%5D.trim())
+;%0A%09%09%09%09context.write(new IntWritable(referral
 ), new IntWritab
@@ -2017,28 +2017,17 @@
 %0A%09%09%09if (
-referralAndP
+p
 ages.len
@@ -2070,27 +2070,23 @@
 s = 
-referralAndPages%5B1%5D
+pages%5B1%5D.trim()
 .spl
@@ -2101,16 +2101,17 @@
 %0A%09%09%09%09for
+ 
 (String 
@@ -2203,23 +2203,16 @@
 rredPage
-.trim()
 )), new 

Corrected the splitting